'Deserializing undefined list [duplicate]

I have some JSON data that I'm receiving from a third party that I can't figure how to deserialize:

{
    "data": {
        "SomeCategory": [
            [
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string"
            ],
            [
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string"
            ]
        ],
        "AnotherRandomCategory": [
            [
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string"
            ],
            [
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string"
            ],
            [
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string"
            ],
            [
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string",
                "some string"
            ]
        ]
    }
}

I'm using the below Model to try and deserialize it, but it doesn't work.

public class ContentClass
{
    [JsonProperty("data")]
    public DataClass Data { get; set; }
}

public class DataClass
{
    [JsonProperty]
    public List<CategoryClass> Category { get; set; }
}

public class CategoryClass
{
    [JsonProperty]
    public List<string> Data { get; set; }
}

Up until the data it deserializes fine. But then I have a dynamically named category that I can't set up in my model, followed by an unnamed list of strings.

Can anyone point me in the right direction? Thanks



Solution 1:[1]

Try this

var content=JsonConvert.DeserializeObject<Content>(json);

public class Content
{
    [JsonProperty("data")]
    public Dictionary <string, List<List<string>>>  Data { get; set; }
}

Solution 2:[2]

Most JSON libraries (JSON.net, System.Text.Json) expects the JSON to be in camel case. And your property names should match the JSON properties - except your c# variables should be Pascal Case. If they do not, or if it is not appropriate, then you can use the JsonProperty attribute to define them.

As an example:

public class DataClass
{
    [JsonProperty("SomeCategory")]
    public List<CategoryClass> Category { get; set; }
}

Further, as noted in the comments, if the data returned is dynamic, map it to a dictionary. It would look like:

public class ContentClass
{
    public Dictionary<string, List<List<string>>> Data
}

No need to set a JsonProperty here as data is in the proper format for c# to assign it into the variable Data in your class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serge
Solution 2