我有一个JSON数据(http://country.io/names.json),如:
{"BD": "Bangladesh", "BE": "Belgium", "BF": "Burkina Faso", "BG": "Bulgaria", "BA": "Bosnia and Herzegovina", "BB": "Barbados" }
我想列出像CountryCode-CountryName(BD-Bangladesh)这样的json。我如何在Windows窗体应用程序上执行此操作。 ?
答案 0 :(得分:1)
您可以将JSON反序列化为Dictionary而不是单个对象。这使您可以访问所有代码和名称,如下所示:
var json = @"{""BD"": ""Bangladesh"", ""BE"": ""Belgium"", ""BF"": ""Burkina Faso"", ""BG"": ""Bulgaria"", ""BA"": ""Bosnia and Herzegovina"", ""BB"": ""Barbados"" }";
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach (var item in dict)
{
var countryCode = item.Key;
var countryName = item.Value;
// do whatever you want to do with those two values here
Console.WriteLine("CountryCode: {0} CountryName: {1}", countryCode, countryName);
}
在该代码中,它只是将其写入屏幕,但很明显,一旦你有了这个循环,你可以用国家代码和名字做任何你喜欢的事情。