-1

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.

Gary K
  • 29
  • 5
  • Does this answer your question? [json deserialization to C# with dynamic keys](https://stackoverflow.com/questions/65727513/json-deserialization-to-c-sharp-with-dynamic-keys) – Charlieface Jul 17 '22 at 03:04

2 Answers2

0

You can deserialise this json into

a class which has a property T(t)ranslations

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

tymtam
  • 31,798
  • 8
  • 86
  • 126
0

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; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45