【问题标题】:Json.NET: Deserializing Json object returns null C# objectJson.NET:反序列化 Json 对象返回空 C# 对象
【发布时间】:2022-01-14 19:18:56
【问题描述】:

我有这个 Json 对象:

{
    "ComplementoCartaPorte": [
        {
            "RFCImportador": "IJD840224QD2",
            "Pedimentos": [
                {
                    "NumPedimento": "80034680000518",
                    "Referencia": "REFPEDIMENTO1IN",
                    "Remesa": 1
                },
                {
                    "NumPedimento": "80034680000528",
                    "Referencia": "REFPEDIMENTO2A1",
                    "Remesa": 0
                }
            ],
            "Archivos": {
                "FolioFiscal": "287d57c7-9132-4234-9268-28e984e7cdf1",
                "PDF": "PD94",
                "XML": "PD94"
            }
        },
        {
            "RFCImportador": "MJD960223MV9",
            "Pedimentos": [
                {
                    "NumPedimento": "80034680000519",
                    "Referencia": "REFPEDIMENTO3IN",
                    "Remesa": 1
                },
                {
                    "NumPedimento": "80034680000520",
                    "Referencia": "REFPEDIMENTO4A1",
                    "Remesa": 0
                }
            ],
            "Archivos": {
                "FolioFiscal": "587d57c7-9133-4234-9268-28e984e7cdg7",
                "PDF": "AM92",
                "XML": "AM92"
            }
        }
    ]
}

我尝试将其反序列化为此类的 C# 对象:

// Top level class
public class ComplementoCartaPorte
{
    // Only one field, an array
    public Complemento[] Complemento { get; set; }
}

// Array elements of "Complemento" field
public class Complemento
{
    public string RFCImportador { get; set; }

    // Array Field
    public Pedimento[] Pedimentos { get; set; }

    public Archivo Archivos { get; set; }
}

// Array elements of "Pedimentos" field
public class Pedimento
{
    public string NumPedimento { get; set; }
    public string Referencia { get; set; }
    public int Remesa { get; set; }
}

public class Archivo
{
    public string FolioFiscal { get; set; }
    public string PDF { get; set; }
    public int XML { get; set; }
}

我尝试像这样反序列化(C#):

ComplementoCartaPorte comp = JsonConvert.DeserializeObject<ComplementoCartaPorte>(json_string);

“comp”结果对象的类型为“ComplementoCartaPorte”,但它只有一个“Complemento”类型的属性,即为空。我希望它是一个包含数据的“Complemento”对象数组。

有人可以解释一下吗?非常感谢。

【问题讨论】:

    标签: c# arrays json.net


    【解决方案1】:

    使用列表 代替 ComplementoCartaPorte

    List<ComplementoCartaPorte> comp = JsonConvert
    .DeserializeObject<Root>(json_string).ComplementoCartaPorte;
    

    public class Pedimento
        {
            public string NumPedimento { get; set; }
            public string Referencia { get; set; }
            public int Remesa { get; set; }
        }
    
        public class Archivos
        {
            public string FolioFiscal { get; set; }
            public string PDF { get; set; }
            public string XML { get; set; }
        }
    
        public class ComplementoCartaPorte
        {
            public string RFCImportador { get; set; }
            public List<Pedimento> Pedimentos { get; set; }
            public Archivos Archivos { get; set; }
        }
    
        public class Root
        {
            public List<ComplementoCartaPorte> ComplementoCartaPorte { get; set; }
        }
    
    

    【讨论】:

    • 这就像一个魅力,非常感谢!另外,感谢您的回答,我现在可以更好地了解这件事是如何工作的。非常感谢。
    【解决方案2】:

    分解上面的 json 可能有助于在这种情况下查看为什么会发生这种情况。

    {} 开始,我们有一个未命名的对象。这是我们的包装器/根对象。

    在那个对象上,我们有一个属性,ComplementoCartaPorte。 这个对象有一个对象数组,用[] 表示,里面有{},{}

    但是这种结构与我们现有的类不一致。 ComplementoCartaPorte 有一个属性,它是一个名为 ComplementoComplemento 对象的数组属性

    如果我们希望将这些对象反序列化为Complemento,那么我们需要稍微调整一下json。

    {
    "ComplementoCartaPorte": {
        "Complemento": [
            {
                "RFCImportador": "IJD840224QD2",
                "Pedimentos": [
                    {
                        "NumPedimento": "80034680000518",
                        "Referencia": "REFPEDIMENTO1IN",
                        "Remesa": 1
                    },
                    {
                        "NumPedimento": "80034680000528",
                        "Referencia": "REFPEDIMENTO2A1",
                        "Remesa": 0
                    }
                ],
                "Archivos": {
                    "FolioFiscal": "287d57c7-9132-4234-9268-28e984e7cdf1",
                    "PDF": "PD94",
                    "XML": "PD94"
                }
            },
            {
                "RFCImportador": "MJD960223MV9",
                "Pedimentos": [
                    {
                        "NumPedimento": "80034680000519",
                        "Referencia": "REFPEDIMENTO3IN",
                        "Remesa": 1
                    },
                    {
                        "NumPedimento": "80034680000520",
                        "Referencia": "REFPEDIMENTO4A1",
                        "Remesa": 0
                    }
                ],
                "Archivos": {
                    "FolioFiscal": "587d57c7-9133-4234-9268-28e984e7cdg7",
                    "PDF": "AM92",
                    "XML": "AM92"
                }
            }
        ]
    }
    

    }

    请注意在打开 ComplementoCartaPorte 对象后为 ComplementoCartaPorte 属性调整对象以及为 Complemento 添加属性。

    然后我们可以添加以下类作为我们的包装器

    public class Wrapper
    {
        public ComplementoCartaPorte ComplementoCartaPorte { get; set; }
    }
    
    Wrapper comp = JsonConvert.DeserializeObject<Wrapper>(json_string);
    

    【讨论】:

    • 很棒的答案,谢谢。我是 JSON 和 JSON.Net 的新手,您的回答帮助我更好地理解这件事是如何工作的。不幸的是,我不能更改 JSON,但可以更改 C# 类。既然我更好地理解了这个主题,我就会走那条路。再次感谢。
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多