[英]How to convert the following JSON string into a dictionary C#
I am trying to convert the following JSON response string into a C# Dictionary<string,string>我正在尝试将以下 JSON 响应字符串转换为 C# Dictionary<string,string>
{
{
"type": "Select",
"name": "timezone",
"label": "Timezone",
"description": "Select the city closest to you that shares your same timezone.",
"multiOptions": {
"US/Pacific": "(UTC-8) Pacific Time (US & Canada)",
"US/Mountain": "(UTC-7) Mountain Time (US & Canada)",
"US/Central": "(UTC-6) Central Time (US & Canada)",
"US/Eastern": "(UTC-5) Eastern Time (US & Canada)",
"America/Halifax": "(UTC-4) Atlantic Time (Canada)",
"America/Anchorage": "(UTC-9) Alaska (US & Canada)",
"Pacific/Honolulu": "(UTC-10) Hawaii (US)",
"Pacific/Samoa": "(UTC-11) Midway Island, Samoa",
"Etc/GMT-12": "(UTC-12) Eniwetok, Kwajalein",
"Canada/Newfoundland": "(UTC-3:30) Canada/Newfoundland",
"America/Buenos_Aires": "(UTC-3) Brasilia, Buenos Aires, Georgetown",
"Atlantic/South_Georgia": "(UTC-2) Mid-Atlantic",
"Atlantic/Azores": "(UTC-1) Azores, Cape Verde Is.",
"Europe/London": "Greenwich Mean Time (Lisbon, London)",
"Europe/Berlin": "(UTC+1) Amsterdam, Berlin, Paris, Rome, Madrid",
"Europe/Athens": "(UTC+2) Athens, Helsinki, Istanbul, Cairo, E. Europe",
"Europe/Moscow": "(UTC+3) Baghdad, Kuwait, Nairobi, Moscow",
"Iran": "(UTC+3:30) Tehran",
"Asia/Dubai": "(UTC+4) Abu Dhabi, Kazan, Muscat",
"Asia/Kabul": "(UTC+4:30) Kabul",
"Asia/Yekaterinburg": "(UTC+5) Islamabad, Karachi, Tashkent",
"Asia/Calcutta": "(UTC+5:30) Bombay, Calcutta, New Delhi",
"Asia/Katmandu": "(UTC+5:45) Nepal",
"Asia/Omsk": "(UTC+6) Almaty, Dhaka",
"India/Cocos": "(UTC+6:30) Cocos Islands, Yangon",
"Asia/Krasnoyarsk": "(UTC+7) Bangkok, Jakarta, Hanoi",
"Asia/Hong_Kong": "(UTC+8) Beijing, Hong Kong, Singapore, Taipei",
"Asia/Tokyo": "(UTC+9) Tokyo, Osaka, Sapporto, Seoul, Yakutsk",
"Australia/Adelaide": "(UTC+9:30) Adelaide, Darwin",
"Australia/Sydney": "(UTC+10) Brisbane, Melbourne, Sydney, Guam",
"Asia/Magadan": "(UTC+11) Magadan, Soloman Is., New Caledonia",
"Pacific/Auckland": "(UTC+12) Fiji, Kamchatka, Marshall Is., Wellington"
}
}
Using Newtonsoft JSON library, I cannot figure out how to do this.使用 Newtonsoft JSON 库,我无法弄清楚如何做到这一点。 Using online converters, it creates a class called MultiOptions, and then puts all of those as properties.使用在线转换器,它创建一个名为 MultiOptions 的类,然后将所有这些作为属性。
You can give Newtonsoft a DTO to deserialize into.你可以给 Newtonsoft 一个 DTO 来反序列化。
var parsedDto = JsonConvert.DeserializeObject<ParsedJsonResponseDto>(jsonResponse);
Where the DTO object looks something like. DTO 对象看起来像的地方。
public class ParsedJsonResponseDto
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("multiOptions")]
public Dictionary<string, string> MultiOptions { get; set; }
}
Now that you have an object with your dictionary of MultiOptions you can iterate over however you like.现在您有了一个带有 MultiOptions 字典的对象,您可以随意迭代。
Yes it's possible , you need to use System.Reflection是的,有可能,您需要使用 System.Reflection
public Root MyDeserializedClass = new Root();
public Dictionary<string, string> MyDictionary = new Dictionary<string, string>();
...
private void Button_Clicked_1(object sender, EventArgs e)
{
ConvertToDictionaryPair();
}
private void ConvertToDictionaryPair()
{
PropertyInfo[] properties = MyDeserializedClass.multiOptions.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
string key = property.Name;
string value = property.GetValue(MyDeserializedClass.multiOptions).ToString();
if (!MyDictionary.ContainsKey(key))
MyDictionary.Add(key, value);
}
}
Model模型
public class Root
{
public string type { get; set; }
public string name { get; set; }
public string label { get; set; }
public string description { get; set; }
public MultiOptions multiOptions { get; set; }
}
public class MultiOptions
{
[JsonProperty("US/Pacific")]
public string USPacific { get; set; }
[JsonProperty("US/Mountain")]
public string USMountain { get; set; }
[JsonProperty("US/Central")]
public string USCentral { get; set; }
[JsonProperty("US/Eastern")]
public string USEastern { get; set; }
[JsonProperty("America/Halifax")]
public string AmericaHalifax { get; set; }
[JsonProperty("America/Anchorage")]
public string AmericaAnchorage { get; set; }
[JsonProperty("Pacific/Honolulu")]
public string PacificHonolulu { get; set; }
[JsonProperty("Pacific/Samoa")]
public string PacificSamoa { get; set; }
[JsonProperty("Etc/GMT-12")]
public string EtcGMT12 { get; set; }
[JsonProperty("Canada/Newfoundland")]
public string CanadaNewfoundland { get; set; }
[JsonProperty("America/Buenos_Aires")]
public string AmericaBuenosAires { get; set; }
[JsonProperty("Atlantic/South_Georgia")]
public string AtlanticSouthGeorgia { get; set; }
[JsonProperty("Atlantic/Azores")]
public string AtlanticAzores { get; set; }
[JsonProperty("Europe/London")]
public string EuropeLondon { get; set; }
[JsonProperty("Europe/Berlin")]
public string EuropeBerlin { get; set; }
[JsonProperty("Europe/Athens")]
public string EuropeAthens { get; set; }
[JsonProperty("Europe/Moscow")]
public string EuropeMoscow { get; set; }
public string Iran { get; set; }
[JsonProperty("Asia/Dubai")]
public string AsiaDubai { get; set; }
[JsonProperty("Asia/Kabul")]
public string AsiaKabul { get; set; }
[JsonProperty("Asia/Yekaterinburg")]
public string AsiaYekaterinburg { get; set; }
[JsonProperty("Asia/Calcutta")]
public string AsiaCalcutta { get; set; }
[JsonProperty("Asia/Katmandu")]
public string AsiaKatmandu { get; set; }
[JsonProperty("Asia/Omsk")]
public string AsiaOmsk { get; set; }
[JsonProperty("India/Cocos")]
public string IndiaCocos { get; set; }
[JsonProperty("Asia/Krasnoyarsk")]
public string AsiaKrasnoyarsk { get; set; }
[JsonProperty("Asia/Hong_Kong")]
public string AsiaHongKong { get; set; }
[JsonProperty("Asia/Tokyo")]
public string AsiaTokyo { get; set; }
[JsonProperty("Australia/Adelaide")]
public string AustraliaAdelaide { get; set; }
[JsonProperty("Australia/Sydney")]
public string AustraliaSydney { get; set; }
[JsonProperty("Asia/Magadan")]
public string AsiaMagadan { get; set; }
[JsonProperty("Pacific/Auckland")]
public string PacificAuckland { get; set; }
}
First click on Get Data and populate the model , then click set data , and iterate through the model and add each property name and value to the dictionary.首先点击获取数据并填充模型,然后点击设置数据,并遍历模型并将每个属性名称和值添加到字典中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.