我有这个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";,但它只有一个类型为"0"的性质;互补";,其为空。我原以为它是一个";互补;具有数据的对象。
有人能解释一下吗。非常感谢。
使用列表<互补CartaPorte>代替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可能有助于了解为什么会发生这种情况。
从{}
开始,我们有一个未命名的对象。这是我们的包装器/根对象。
在这个对象上,我们有一个属性ComplementoCartaPorte
。这个对象有一个对象数组,如[]
所示,里面有{},{}
。
但这种结构与我们现有的类不一致。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属性的对象进行了调整—打开ComplementoCartaPorte
对象后添加了Complemento
的属性。
然后我们可以添加以下类作为我们的包装
public class Wrapper
{
public ComplementoCartaPorte ComplementoCartaPorte { get; set; }
}
Wrapper comp = JsonConvert.DeserializeObject<Wrapper>(json_string);