简体   繁体   中英

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.

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

a class which has a property T(t)ranslations

and this property is a 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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