在JSON.NET中反序列化具有不同名称的对象列表 [英] Deserializing a list of objects with different names in JSON.NET

查看:82
本文介绍了在JSON.NET中反序列化具有不同名称的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个网站获取数据,该网站返回我不熟悉的.json格式.我一直在寻找解决方案几个小时,而且我必须使用术语.

json的格式如下:

[
{
    "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
        }
    }
}]

我正在使用Newtonsoft的JSON.NET,而且似乎无法创建允许我对其进行反序列化的数据结构,因为它是具有不同名称的类的数组.具体来说,属性名称"Foo""bar"在运行时可能会有所不同. JSON层次结构中其他地方的属性名称是已知的.

解决方案

假定在编译时只有名称"Foo""Bar"未知,则可以将该JSON反序列化为List<Dictionary<string, RootObject>>,其中RootObject是我使用 http://json2csharp.com/从JSON中为"Foo"值自动生成的ac#模型

型号:

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);

注意:

  • 最外面的类型必须是可枚举的List<T>,因为最外面的JSON容器是一个数组-由[]包围的逗号分隔的值序列.请参见序列化指南:IEnumerable,列表和数组.. p>

  • 当JSON对象可以具有任意属性名称,但具有固定的属性值架构时,可以将其反序列化为Dictionary<string, T>以获取适当的T.请参见反序列化字典.

  • 可能bytesepoch的类型应为long.

正在工作 .Net小提琴.

I'm getting my data from a website which returns a .json format that is quite unfamiliar to me. I've been looking for the solution for a couple of hours, and I must be using the terminology.

The 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'm using Newtonsoft's JSON.NET, and I can't seem to be able to create a data structure that would allow me to deserialize it, since it's the array of classes with different names. Specifically the property names "Foo" and "bar" could differ at runtime. Property names elsewhere in the JSON hierarchy are known.

解决方案

Assuming that only the names "Foo" and "Bar" are unknown at compile time, you can deserialize that JSON into a List<Dictionary<string, RootObject>>, where RootObject is a c# model I generated automatically using http://json2csharp.com/ from the JSON for the value of "Foo".

Models:

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; }
}

Deserialization code:

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

Notes:

  • The outermost type must be an enumerable such List<T> since the outermost JSON container is an array -- a comma-separated sequence of values surrounded by [ and ]. See Serialization Guide: IEnumerable, Lists, and Arrays.

  • When a JSON object can have arbitrary property names but a fixed schema for property values, it can be deserialized to a Dictionary<string, T> for an appropriate T. See Deserialize a Dictionary.

  • Possibly bytes and epoch should be of type long.

Working .Net fiddle.

这篇关于在JSON.NET中反序列化具有不同名称的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆