1 year ago
#385128
WMM
Deserialize JSON with multiple (nested) unknown keys
I have the following Json that i would like to deserialize into classes. Basically the most outer key is dynamic, can be id1 upto id(x). The innerid is also dynamic (innerid1 to innerid(x)). The only keys that are not dynamic are the keys for history, valid and date. I have tried to create a class for the outer one but it did not work.
{
"id1": {
"innerid1": {
"history": [{
"id": "pid1",
"key": 1
},
{
"id": "pid2",
"key": 2
}
]
},
"innerid2": {
"history": [{
"id": "pid2",
"key": 1
},
{
"id": "pid1",
"key": 2
}
]
},
"valid": true,
"date": "datetime"
},
"id2": {
"innerid1": {
"history": [{
"id": "pid1",
"key": 1
},
{
"id": "pid2",
"key": 2
}
]
},
"valid": true,
"date": "datetime"
},
"id3": {
}
}
public class OuterId {
public Dictionary<string, Items> info { get; set; }
}
public class Items{
public Dictionary<string, List<History>> history{ get; set; }
public bool valid {get; set;}
public string date {get; set;}
}
public class History{
public string id {get; set;}
public string key {get; set;}
}
I deserialize it with the following but it didn't work, it is just null
JsonConvert.DeserializeObject<OuterId>(Json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });
Then i tried to deserialize it with the below, it worked but i am only able to get the key, and values for valid and date. My history array doesn't populate.
JsonConvert.DeserializeObject<Dictionary<string,Items>>(Json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });
Then to get the history array i tried to deserialize it below but did not work as well
JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, List<History>>>>(CreativeAccount.Instance.json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });
I tried another method by making another class to contain the list for History, which worked to populate the history array but i am unable to get the other values like valid and date from the outerId
public class HistoryInfo{
public List<History> History {get; set;}
}
JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, HistoryInfo>>>(CreativeAccount.Instance.json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });
Since the above worked to get the history array i updated the Items class to try and get the array with my outerId values but it still doesn't work. I am only able to deserialize the values inside the outerId and history array separately.
public class Items{
public Dictionary<string, HistoryInfo> history{ get; set; }
public bool valid {get; set;}
public string date {get; set;}
}
JsonConvert.DeserializeObject<OuterId>(Json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore });
Is there something wrong with the classes structure? I can't really do anything about the json since it is not updated by me. Any help is appreciated. Thank you
c#
json
dictionary
deserialization
json-deserialization
0 Answers
Your Answer