C# 将JSON对象反序列化到类中

C# 将JSON对象反序列化到类中,c#,json.net,json-deserialization,C#,Json.net,Json Deserialization,我在将JSON对象反序列化到类(使用JSON.NET)时遇到了一些麻烦,希望有人能给我指出正确的方向。下面是我正在尝试的代码片段,并且一直在测试 下面是JSON的一个示例: { "`LCA0001": { "23225007190002": "1", "23249206670003": "1", "01365100070018": "5" }, "`LCA0003": { "23331406670018":

我在将JSON对象反序列化到类(使用JSON.NET)时遇到了一些麻烦,希望有人能给我指出正确的方向。下面是我正在尝试的代码片段,并且一直在测试

下面是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; }
}
使用系统;
使用System.Collections.Generic;
使用Newtonsoft.Json;
公共课程
{
公共静态void Main()
{
字符串json=“{\”`LCA001\”:{\“23225007190002\”:“1\”,“232492066700003\”:“1\”,“013651000018\”:“5\”,“`LCA00003\”:{\“23331406670018\”:“1\”,“24942506670004\”:“1\”,“`LCA0005\”:{“013651000018\”:“19\”;
Console.WriteLine(json);
Console.WriteLine();
//这很有效
Write(“无类反序列化”);
var root=JsonConvert.DeserializeObject(json);
foreach(根目录中的var locationKvp)
{
foreach(位置KVP.Value中的var skuKvp)
{
Console.WriteLine(“位置:+locationKvp.Key+”,sku:+skuKvp.Key+”,数量:+skuKvp.Value);
}
}
//为什么这样不行?
Console.Write(“\n使用类序列化”);
var root2=JsonConvert.DeserializeObject(json);
foreach(root2.InventoryLocation中的var locationKvp)
{
foreach(位置KVP.Value中的var skuKvp)
{
Console.WriteLine(“位置:+locationKvp.Key+”,sku:+skuKvp.Key+”,数量:+skuKvp.Value);
}
}
}
}
类别目录位置
{
公共字典目录位置{get;set;}
}
公共类位置
{
公共字符串位置{get;set;}
}
公共类Sku
{
公共字符串sku{get;set;}
}
公共类数量
{
公共整数数量{get;set;}
}

反序列化到类中不起作用有什么原因吗?我只是定义了不正确的类吗?

我在这里看到了两个问题:一个是使用类作为字典键-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 class
。对于所发布的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在类中序列化/反序列化字典

注意,带有契约解析器的序列化Json与原始Json不同。必须使用此协定解析程序对其进行序列化,才能使用它进行反序列化


如果您需要更多的说明,我从中提取了合同解析程序。

您的原因是它们不是相同的反序列化?(
Dictionary>
vs.用类代替字符串(JSON与之不匹配)。看起来像是第二个示例(不起作用的示例)嵌套更深一层。在第一个示例中,如果反序列化为Dictionary而不是Dictionary,它会起作用吗?向上一个…展示如何通过将JSON粘贴为类来提高生产率。附带说明:只有在编辑代码文件时,才可以看到将JSON粘贴为类。请参见以下答案:虽然粘贴为类很好,但似乎不太好对于这个问题有任何价值,因为“名称”似乎是可变的,而不是固定的类名和属性。谢谢你的提示。我通常使用在线工具,比如json2charp.com,没有意识到你可以直接在vs.Ahh中这样做,这是我不理解的。谢谢你的澄清。