Autonomic
Autonomic

Reputation: 170

Deserialize a JSON dictionary

I have an issue, I cannot figure out how to deserialize this JSON. I've tried a number of things, from Dictionary, to Lists. I know the item I'm after is a dataset and I tried that also, nothing works. This is the JSON structure

{
"myRoot": {
    "myList": [
        {
            "data1": "somedata",
            "data2": "somedata"
        },
        {
            "data1": "somedata",
            "data2": "somedata"
        },
        {
            "data1": "somedata",
            "data2": "somedata"
        },
        {
            "data1": "somedata",
            "data2": "somedata"
        },
    ]
   }
}

These are the classes that are not working

private class WTH
{
    [JsonProperty("myList")]
    public List<Codes> mystuff { get; set; }
}
private class Codes
{
    [JsonProperty("data2")]
    public string data2{ get; set; }
}

My deserialization that either blows up, or ends up null.

WTH summary = JsonConvert.DeserializeObject<WTH>(response.Content);

Upvotes: 0

Views: 93

Answers (1)

DavidG
DavidG

Reputation: 119116

You need another wrapper class around that with a myRoot property. For example:

public class Rootobject
{
    [JsonProperty("myRoot")]
    public WTH WTH{ get; set; }
}

public class WTH
{
    [JsonProperty("myList")]
    public List<Codes> mystuff { get; set; }
}

public class Codes
{
    public string data1 { get; set; }
    public string data2 { get; set; }
}

And deserialise like this:

var summary = JsonConvert.DeserializeObject<Rootobject>(response.Content);

Upvotes: 1

Related Questions