Reputation: 405
I'm having to process 3rd party json data. I'm trying to use JSON.Net, but I'm struggling a little as, in the data, the same type is given a different name every time. See example below.
{
"success":"1",
"return":{
"Mike":{
"name":"Mike",
"age":"21",
"hobbies":[
{
"name":"sailing"
},
{
"name":"volleyball"
}
]
}
}
}
Here you can see that - in this made up example to illustrate the situation - basically a person object is returned, but it is called "Mike" not person. The next might be called "Sheryl", etc. I would like to just deserialise the whole thing in one go using: var deserialized = JsonConvert.DeserializeObject(jsonString);
However I'm not sure how to build x as it can vary.
I've looked at JsonConverter, but I can't see how that would help in this situation.
Any guidance is much appreciated.
Upvotes: 0
Views: 674
Reputation: 116118
Use Dictionary<string,Person>
for property Return
var obj = JsonConvert.DeserializeObject<YourObject>(json);
public class Hobby
{
public string Name { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
public List<Hobby> Hobbies { get; set; }
}
public class YourObject
{
public string Success { get; set; }
public Dictionary<string,Person> Return { get; set; }
}
Upvotes: 3