将 JSON 对象反序列化为 Class

Deserialize JSON Object into Class

我在将 JSON 对象反序列化为 class(使用 JSON.NET)时遇到了一些问题,希望有人能为我指出正确的方向。下面是我正在尝试并在 dotnetfiddle

测试的代码片段

这是 JSON 的示例:

{
    "`LCA0001": {
        "23225007190002": "1",
        "23249206670003": "1",
        "01365100070018": "5"
    },
    "`LCA0003": {
        "23331406670018": "1",
        "24942506670004": "1"
    },
    "`LCA0005": {
        "01365100070018": "19"
    }
}

我正在尝试使用此代码:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {

        string json = "{\"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}";
        Console.WriteLine(json);
        Console.WriteLine();

        //This works
        Console.Write("Deserialize without class");
        var root = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, int>>>(json);
        foreach (var locationKvp in root)
        {
            foreach (var skuKvp in locationKvp.Value)
            {
                Console.WriteLine("location: " + locationKvp.Key + ", sku: " +  skuKvp.Key + ", qty: " + skuKvp.Value);
            }
        }

        //Why doesn't this work?
        Console.Write("\nDeserialize with class");      
        var root2 = JsonConvert.DeserializeObject<InventoryLocations>(json);
        foreach (var locationKvp in root2.InventoryLocation)
        {
            foreach (var skuKvp in locationKvp.Value)
            {
                Console.WriteLine("location: " + locationKvp.Key + ", sku: " +  skuKvp.Key + ", qty: " + skuKvp.Value);
            }
        }
    }
}

class InventoryLocations
{
    public Dictionary<Location, Dictionary<Sku, Qty>> InventoryLocation { get; set; }
}

public class Location
{
    public string location { get; set; }
}

public class Sku
{
    public string sku { get; set; }
}

public class Qty
{
    public int qty { get; set; }
}

为什么反序列化为 Class 不起作用?我只是错误地定义了 classes 吗?

我在这里看到两个问题:一个是使用 类 作为字典键 - JSON 那里有简单的字符串(实际上不能有任何其他内容),所以这行不通。

第二个问题是 JSON 到 类 的反序列化是通过将键与属性进行匹配来实现的 - 所以它会转换为

{
    "prop1": "value1",
    "prop2": "value2"
}

到一个实例:

public class MyClass {
   public string prop1 { get; set; }
   public string prop2 { get; set; }
}

在您的情况下,这不起作用,因为在您的 JSON 中,所有键都不是有效的 属性 名称。你必须坚持反序列化到字典

从 JSON 生成 类 的方法之一是使用 Visual Studio.

导航至 Edit -> Paste Special -> Paste JSON As Classes。对于发布的 JSON 问题,生成以下 类。

public class Rootobject
{
    public LCA0001 LCA0001 { get; set; }
    public LCA0003 LCA0003 { get; set; }
    public LCA0005 LCA0005 { get; set; }
}

public class LCA0001
{
    public string _23225007190002 { get; set; }
    public string _23249206670003 { get; set; }
    public string _01365100070018 { get; set; }
}

public class LCA0003
{
    public string _23331406670018 { get; set; }
    public string _24942506670004 { get; set; }
}

public class LCA0005
{
    public string _01365100070018 { get; set; }
}

除了 MiMo 的答案之外,您还可以使用 ContractResolver 到 类 中的 serialize/deserialize 个字典。

Here's a working example of your code in dotnetfiddle.

请注意,带有合同解析器的序列化 Json 与原始 json 不同。必须使用此合同解析器对其进行序列化才能对其进行反序列化。

如果您需要更多说明,我从 this Whosebug question 中提取了合同解析器。