将键值对反序列化为列表(Deserializing key value pair into list)
任何人都可以告诉我如何将键值对反序列化到列表中? 即从JSON响应,我想从下面的响应中获取国家和代码列表。 我正在使用Newtonsoft.Json:
{ "status": "SUCCESS", "message": "something", "data": { "trade_origin_iso3country": "GBR", "countries": { "ARM ": "Armenia", "BLR": "Belarus ", "DNK": "Denmark", "GBR": "United Kingdom", "MCO": "Monaco" } } }Can anyone please show me how to deserialize key-value pair into a list? i.e. From the JSON response, I want to get out the list of countries and code from below response. I am using Newtonsoft.Json:
{ "status": "SUCCESS", "message": "something", "data": { "trade_origin_iso3country": "GBR", "countries": { "ARM ": "Armenia", "BLR": "Belarus ", "DNK": "Denmark", "GBR": "United Kingdom", "MCO": "Monaco" } } }最满意答案
如果可能,我推荐自定义类型。 这是一个控制台应用程序,您可以复制和粘贴,使用您提供的信息向您显示我所指的内容。 它使用众所周知且极力推荐的Newtonsoft.Json nuget包。
码:
using Newtonsoft.Json; using System; using System.Collections.Generic; namespace Question_Answer_Console_App { public class Program { public const string JsonResultString = @"{ ""status"": ""SUCCESS"", ""message"": ""something"", ""data"": { ""trade_origin_iso3country"": ""GBR"", ""countries"": { ""ARM"": ""Armenia"", ""BLR"": ""Belarus "", ""DNK"": ""Denmark"", ""GBR"": ""United Kingdom"", ""MCO"": ""Monaco"" } } }"; [STAThread] static void Main(string[] args) { var jsonResult = JsonConvert.DeserializeObject<JsonResult>(JsonResultString); foreach (var keyValue in jsonResult.Data.Countries) Console.WriteLine($"{keyValue.Key} : {keyValue.Value}"); Console.Read(); } } public class JsonResult { public string Status { get; set; } public string Message { get; set; } public JsonData Data { get; set; } } public class JsonData { [JsonProperty("trade_origin_iso3country")] public string TradeOriginIso3country { get; set; } public Dictionary<string, string> Countries { get; set; } } }输出:
ARM : Armenia BLR : Belarus DNK : Denmark GBR : United Kingdom MCO : MonacoI recommend custom types if that's possible. Here's a console app you can copy and paste that shows you what I'm referring to using the information you've provided. It uses the well known and highly recommended Newtonsoft.Json nuget package.
Code:
using Newtonsoft.Json; using System; using System.Collections.Generic; namespace Question_Answer_Console_App { public class Program { public const string JsonResultString = @"{ ""status"": ""SUCCESS"", ""message"": ""something"", ""data"": { ""trade_origin_iso3country"": ""GBR"", ""countries"": { ""ARM"": ""Armenia"", ""BLR"": ""Belarus "", ""DNK"": ""Denmark"", ""GBR"": ""United Kingdom"", ""MCO"": ""Monaco"" } } }"; [STAThread] static void Main(string[] args) { var jsonResult = JsonConvert.DeserializeObject<JsonResult>(JsonResultString); foreach (var keyValue in jsonResult.Data.Countries) Console.WriteLine($"{keyValue.Key} : {keyValue.Value}"); Console.Read(); } } public class JsonResult { public string Status { get; set; } public string Message { get; set; } public JsonData Data { get; set; } } public class JsonData { [JsonProperty("trade_origin_iso3country")] public string TradeOriginIso3country { get; set; } public Dictionary<string, string> Countries { get; set; } } }Outputs:
ARM : Armenia BLR : Belarus DNK : Denmark GBR : United Kingdom MCO : Monaco更多推荐
发布评论