Removing deserialization of a list of objects with different names in JSON.NET

I get my data from a website that returns a .json format that is unfamiliar to me. I have been looking for a solution for several hours, and I have to use terminology.

json is formatted something like this:

[
{
    "Foo": {
        "name": "Foo",      
        "size": {
            "human": "832.73kB",
            "bytes": 852718
        },
        "date": {
            "human": "September 18, 2017",
            "epoch": 1505776741
        },
    }
},
{
    "bar": {
        "name": "bar",
        "size": {
            "human": "4.02MB",
            "bytes": 4212456
        },
        "date": {
            "human": "September 18, 2017",
            "epoch": 1505776741
        }
    }
}]

I am using Newtonsoft JSON.NET and I cannot create a data structure that would allow me to deserialize it, since it is an array of classes with different names. In particular, property names "Foo"and "bar"may differ at run time. Property names elsewhere in the JSON hierarchy are known.

+4
source share
1 answer

, "Foo" "Bar", JSON List<Dictionary<string, RootObject>>, RootObject - a# , , http://json2csharp.com/ JSON "Foo".

:

public class Size
{
    public string human { get; set; }
    public int bytes { get; set; }
}

public class Date
{
    public string human { get; set; }
    public int epoch { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public Size size { get; set; }
    public Date date { get; set; }
}

:

var list = JsonConvert.DeserializeObject<List<Dictionary<string, RootObject>>>(jsonString);

:

. .

+5

Source: https://habr.com/ru/post/1686167/


All Articles