【问题标题】:How to deserialise complex JSON with JSON.net c#如何使用 JSON.net c# 反序列化复杂的 JSON
【发布时间】:2017-09-22 16:24:09
【问题描述】:

我有带有字符串键字典和值数组的 JSON。下面是他的结构:

{
"NameA":{
    "ParametersA":[
        [104.3,5.368783],
        [104.212,2.57357],...,
    ],
    "ParametersB":[
        [104.3,5.368783],
        [104.212,2.57357],...,
    ]
},
"NameB":{
    "ParametersA":[
        [104.3,5.368783],
        [104.212,2.57357],...,
    ],
    "ParametersB":[
        [104.3,5.368783],
        [104.212,2.57357],...,
        ]
    },
"ThousandsNamesN":{[...]
    }
}

我为此创建类以获取如下数据:Dictionaty<key=NameA, value=List<Parameters>> 和参数是另一个具有数组 A 和 B 的类。

这是我的根类:

    internal class RawDepth
{
    public Dictionary<string, Parameters> Names { get; set; }

    internal class Parameters
    {
        [JsonProperty("ParametersA")]
        public IList<Orders> A { get; set; }


        [JsonProperty("ParametersB")]
        public IList<ParamsArray> B { get; set; }
    }

    internal class ParamsArray
    {
        public decimal[,] _Orders { get; set; }
    }
}

我捕获了一个空引用异常。我尝试以不同的方式创建类,但我仍然无法反序列化它。我做错了什么?

【问题讨论】:

  • 分开你的班级。

标签: c# json json.net deserialization


【解决方案1】:

你的模型可以是这样的

public class Parameters
{
    public List<List<decimal>> ParametersA { get; set; }
    public List<List<decimal>> ParametersB { get; set; }
}

现在你可以反序列化为

var dict = JsonConvert.DeserializeObject<Dictionary<string, Parameters>>(json);

【讨论】:

    【解决方案2】:

    有一个很棒的工具json2csharp 可以让您粘贴 JSON 并从中生成 C# 类。这就是我从你的样本中得到的:

    public class NameA
    {
        public List<List<double>> ParametersA { get; set; }
        public List<List<double>> ParametersB { get; set; }
    }
    
    public class NameB
    {
        public List<List<double>> ParametersA { get; set; }
        public List<List<double>> ParametersB { get; set; }
    }
    
    public class RootObject
    {
        public NameA NameA { get; set; }
        public NameB NameB { get; set; }
        public List<string> ThousandsNamesN { get; set; }
    }
    

    我认为我们可以放心地假设 NameA 和 NameB 类是相同的。

    我必须稍微编辑一下 JSON 才能让它工作,所以我将它粘贴在下面:

        {
    "NameA":{
        "ParametersA":[
            [104.3,5.368783],
            [104.212,2.57357],
        ],
        "ParametersB":[
            [104.3,5.368783],
            [104.212,2.57357],
        ]
    },
    "NameB":{
        "ParametersA":[
            [104.3,5.368783],
            [104.212,2.57357],
        ],
        "ParametersB":[
            [104.3,5.368783],
            [104.212,2.57357],
            ]
        },
    "ThousandsNamesN":["name1", "name2"]
    
    }
    

    所以在考虑了你最初的想法之后,你的代码应该是这样的:

    public class Order
    {
        public List<List<double>> ParametersA { get; set; }
        public List<List<double>> ParametersB { get; set; }
    }
    
    var obj = JsonConvert.DeserializeObject<Dictionary<string, Order>>(json);
    

    【讨论】:

    • 我知道,使用相同的服务。但我有数千个键 NameA、NameB、...NameN,其中 N 可以是 3000-4000。早期我将相同的 JSON 反序列化到 Dictionary 其中 NameA..N 在另一个对象中,但是在这个 JSON 中我遇到了麻烦......
    • 我已经编辑了我的答案。你的根对象是一本字典
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多