Home > Software engineering >  What C# data type would you de-serialize this Json into
What C# data type would you de-serialize this Json into

Time:07-16

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.

CodePudding user response:

You can deserialise this json into

a class which has a property T(t)ranslations

and this property is a Dictionary<string, SomeClass>.

CodePudding user response:

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; }
}
  • Related