Chamov Chamov - 2 months ago 8
C# Question

Check if user is on list

I have Array in JSON file. File looks like this:

["Maverick", "rick", "Rick", "prick", "rick_07"]


I have a username. I want to check if this username is in Array.

public string UserToCheck = "rick";


So im reading json file from URL...

using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString("http://example.ex/users.json");
// Here I want to check if user is on list
}
}


But how Can I check if "UserToCheck" exactly match one of users from array?

Answer

You could parse your Json with the great Newtonsoft Json Library:

var users = JsonConvert.DeserializeObject<List<string>>(json);    
users.Contains(UserToCheck);

As this is case sensitive, you could use LINQ: users.Any(u => String.Equals(u, UserToCheck, StringComparison.OrdinalIgnoreCase))