Stack Overflow:
I am using JSON.NET and have done so successfully before, however I am now running into a nasty bit of JSON that I cannot seem to deserialize. Clearly the problem lays in how the class is constructed, and so I kindly ask for someone to help me plan a class that JSON.NET can successfully deserialize into.
Here is the structure of the JSON string:
[
{
"name":"",
"cost":"",
"cmc":"",
"loyalty":"",
"supertype":"",
"type":"",
"subtype":"",
"power":"",
"toughness":"",
"hand":"",
"life":"",
"rule":"",
"multi":"",
"set":
[ // Note: there could be an infinite number of sets
{
"setcode":"",
"rarity":"",
"number":"",
"artist":"",
"flavor":""
},
{ "setcode":"",
"rarity":"",
"number":"",
"artist":"",
"flavor":""
},
]
}
]
Thank you very much, Stack Overflow
You’ll want to have a class with properties for all those names. When it comes to the array, it’s really an array of objects so you need to create another class with same named properties. Something like:
Also, from your JSON it looks like it’s an array already so you’ll want to deserialize it as an array:
That will give you an array of your deserialized MainObject just like your JSON is an array.