I have this 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"
}
}
]
}
I try to de-serialize it to a C# object of this class:
// 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; }
}
I try to de-serialize like this (C#):
ComplementoCartaPorte comp = JsonConvert.DeserializeObject<ComplementoCartaPorte>(json_string);
The "comp" resulting object is of type "ComplementoCartaPorte", but it has only one property of type "Complemento", which is null. I expected it to be an array of "Complemento" objects with data.
Can somebody please shed some light on this. Thank you very much.
CodePudding user response:
Use List < ComplementoCartaPorte > instead of ComplementoCartaPorte
List<ComplementoCartaPorte> comp = JsonConvert
.DeserializeObject<Root>(json_string).ComplementoCartaPorte;
classes
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; }
}
CodePudding user response:
Breaking down the json above might be helpful to see in this case to see why this is happening.
Starting with {}
we have an unnamed object. This is our wrapper / root object.
On that object we have a property, ComplementoCartaPorte
.
This object has an array of objects as denoted by []
with {},{}
inside.
But this structure doesn't align with our existing classes.
ComplementoCartaPorte
has a property that is an array property called Complemento
of Complemento
objects
If we want those objects to be deserialized as Complemento
then we need to adjust the json a bit.
{
"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"
}
}
]
}
}
Notice the adjusting of the object for the ComplementoCartaPorte property an addition of the property for Complemento
after opening the ComplementoCartaPorte
object.
Then we can add the following class to be our wrapper
public class Wrapper
{
public ComplementoCartaPorte ComplementoCartaPorte { get; set; }
}
Wrapper comp = JsonConvert.DeserializeObject<Wrapper>(json_string);