繁体   English   中英

Json.NET: Deserializing Json object returns null C# object

[英]Json.NET: Deserializing Json object returns null C# object

我有这个 Json object:

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

我尝试将其反序列化为 class 的 C# object:

// 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”生成的 object 属于“ComplementoCartaPorte”类型,但它只有一个“Complemento”类型的属性,即 null。 我希望它是一个带有数据的“Complemento”对象数组。

有人可以对此有所了解。 非常感谢。

使用 List < ComplementoCartaPorte > 而不是 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; }
    }

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

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

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

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

如果我们希望将这些对象反序列化为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 object 后,对Complemento属性的 object 的调整以及对 Complemento 的属性的添加。

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

public class Wrapper
{
    public ComplementoCartaPorte ComplementoCartaPorte { get; set; }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM