KreminT KreminT - 1 month ago 10
C# Question

C# json deserialization format ("1": {"Id": "1"})

I get response from resource in format

{"data": {
"1": {"Id": "1"},
"2": {"Id": "2"}
}}


i must deserialize these in simple object list. How i can do it,if "1","2" it's dynamic integer values?
I need model for deserialization.

Answer

you could use Json.Net for deserialization with this model:

class JsonModelContainer
{
    public Dictionary<string, JsonModel> Data { get; set; }
}

class JsonModel
{
    public string Id { get; set;}
}

usage:

var model = JsonConvert.DeserializeObject<JsonModelContainer>(jsonString);