This is what I downloaded from GitHub: https://gist.githubusercontent.com/Keeguon/2310008/raw/bdc2ce1c1e3f28f9cab5b4393c7549f38361be4e/countries.json and https://raw.githubusercontent.com/dominictarr/random-name/master/first-names.json
I have two textblocks in my application and I want to check whether the Country name and First name entered exists in the list or not.
I want the application to be offline so I downloaded those files. Really new with JSON.
And is there any other option to populate a list of Names Countries etc?
Update: I have tried loads of stuff including the code in Mathew's answer below. But I get the following message
Unhandled exception at 0x75A86ABE (combase.dll) in Name Place Animal Thing.exe: 0xC000027B: An application-internal exception has occurred (parameters: 0x097EF758, 0x00000002).
I tried something like this:
private void newbt_Click(object sender, RoutedEventArgs e)
{
List<Places> myPlaces = JsonConvert.DeserializeObject<List<Places>>("Assets/countries.json");
bool zimbabwe = myPlaces.Any(x => x.name == "Zimbabwe");
if (zimbabwe)
NTB.Text = "";
}
JSON.net is the best tool I know of for manipulating json. Use nuget to add json.net to your project then you can convert the json to a list like this:
List<Country> countries = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>>(json);
which requires that you have this class defined in your project:
public class Country
{
public string name { get; set; }
public string code { get; set; }
}
and you can check if the country is in the list using linq with lambda expressions:
bool zimbabwe = countries.Any(x=>x.name=="Zimbabwe");//true
bool fakecountry = countries.Any(x => x.name == "fake");//false
To answer your question
And is there any other option to populate a list of Names Countries etc?
Yes, there are other options. JSON.net also supports JSONPath which is basically a carbon copy of XPath but for JSON instead of XML. You might have trouble with that though because it looks like your json is not valid json (property names should be in quotes like this: {'name':'Zimbabwe','code':'ZW'}
)
User contributions licensed under CC BY-SA 3.0