2016-04-06 70 views

回答

1

您可以將JSON反序列化爲字典而不是單個對象。這使您可以訪問所有的代碼和名稱,像這樣:

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); 
} 

在該代碼中它只是將其寫入到屏幕上,但很明顯,一旦你有一個循環的地方,你可以做任何你與該國希望代碼和名稱。