简体   繁体   English

你会将这个 Json 反序列化成什么 C# 数据类型

[英]What C# data type would you de-serialize this Json into

What kind of C# data type / List / Class would I de-serialise the following json into when the 3-character codes (ara, ces, cym, dei etc) are dynamic.当 3 字符代码(ara、ces、cym、dei 等)是动态的时,我会将以下 json 反序列化为哪种 C# 数据类型/列表/类。

Thanks!谢谢!

"translations":{
"ara":{"official":"مملكة هولندا","common":"هولندا"},
"ces":{"official":"Nizozemské království","common":"Nizozemsko"},
"cym":{"official":"Kingdom of the Netherlands","common":"Netherlands"},
"deu":{"official":"Niederlande","common":"Niederlande"},
"est":{"official":"Madalmaade Kuningriik","common":"Holland"}
}"

When I say dynamic - at run time you don't know what they are or how many you will be returned.当我说动态时——在运行时你不知道它们是什么或者你会返回多少。

You can deserialise this json into您可以将此 json 反序列化为

a class which has a property T(t)ranslations具有属性T(t)ranslations的类

and this property is a Dictionary<string, SomeClass> .这个属性是Dictionary<string, SomeClass>

I would try something like this我会尝试这样的事情

Dictionary<string,Country> translations=   JsonConvert.DeserializeObject<Data>(json).translations;

CountryName ara = translations["ara"];

//or

string officialDeu = translations["deu"].official; // Niederlande

public class Data
{
    public Dictionary<string,CountryName> translations { get; set; }
}

public class CountryName
{
    public string official { get; set; }
    public string common { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM