I am trying to put together a json with the following structure
{
"CardCode": "C003",
"DocumentLines": [
{
"ItemCode": "A004",
"Quantity": "100",
"TaxCode": "IVA",
"UnitPrice": "500",
"DiscountPercent": 10
},
{
"ItemCode": "A005",
"Quantity": "300",
"TaxCode": "IVA",
"UnitPrice": "250",
"DiscountPercent": 10
}
]
}
but when using the code in my class I get an error in the part where I declare the array since this json can receive multiple items to send it to the server
The class I have is this
public partial class Oferta
{
[JsonProperty("CardCode")]
public string CardCode { get; set; }
[JsonProperty("DocumentLines")]
public List<DocumentLine> DocumentLines { get; set; }
}
public partial class DocumentLine
{
[JsonProperty("ItemCode")]
public string ItemCode { get; set; }
[JsonProperty("Quantity")]
public decimal Quantity { get; set; }
[JsonProperty("TaxCode")]
public string TaxCode { get; set; }
[JsonProperty("UnitPrice")]
public decimal UnitPrice { get; set; }
[JsonProperty("DiscountPercent")]
public decimal DiscountPercent { get; set; }
}
And the code I use to build the json is this
var oferta = new Oferta();
oferta.CardCode = pedido.CodigoSAP_Cliente;
foreach (var item in listaPedido)
{
oferta.DocumentLines = new List<DocumentLine>
{
new DocumentLine
{
ItemCode = item.Codigo,
DiscountPercent = item.Descuento,
TaxCode = "IVA",
Quantity = item.Cantidad,
UnitPrice = item.Precio
}
};
}
string ofertaJson = JsonConvert.SerializeObject(oferta, Formatting.Indented);
I found this on the web but it is not working for me and I have been searching and I get similar things
This:
It can be written: