简体   繁体   中英

C# Generic List to Json nested objects

I'd like to know how I can use C# to export a C# Generic List into a JSON object that is using nested objects, so not a Json Array.

The Json I'd like to generate should look like this:

{
  "sales_invoice": {
    "contact_id": 201171400939013415,
    "remove_invoice_sequence_id": true,
    "invoice_sequence_id": "F2017004",
    "reference": null,
    "invoice_date": "5-1-2017",
    "currency": "EUR",
    "prices_are_incl_tax": false,
    "details_attributes": {
      "0": {
        "description": "Product description",
        "price": 13.80,
        "amount": "10",
        "tax_rate_id": 197923875808347751,
        "ledger_account_id": 197923875101607508
      },
      "1": {
        "description": "Product description",
        "price": 13.80,
        "amount": "10",
        "tax_rate_id": 197923875808347751,
        "ledger_account_id": 197923875101607508
      }
    }
  }
}

This are my C# classes including the Json properties:

 [JsonObject(Title = "sales_invoice")]
    public class SalesInvoice
    {
        [JsonProperty("contact_id")]
        public long ContactId { get; set; }

        [JsonProperty("remove_invoice_sequence_id")]
        public bool RemoveInvoiceSequenceId { get; set; }

        [JsonProperty("invoice_sequence_id")]
        public string InvoiceSequenceId { get; set; }

        [JsonProperty("reference")]
        public string Reference { get; set; }

        [JsonProperty("invoice_date")]
        public string InvoiceDate { get; set; }

        [JsonProperty("currency")]
        public string Currency { get; set; }


        [JsonProperty("prices_are_incl_tax")]
        public bool PricesAreIncludingTax { get; set; }


        [JsonProperty("details_attributes")]
        public List<SalesInvoiceDetails> Details { get; set; }

    }

    public class SalesInvoiceDetails
    {

        [JsonProperty("0")]
        public SalesInvoiceDetailAttributes SalesInvoiceDetail { get; set; }
    }

    public class SalesInvoiceDetailAttributes
    {

        [JsonProperty("description")]
        public string Description { get; set; }


        [JsonProperty("price")]
        public decimal Price { get; set; }


        [JsonProperty("amount")]
        public string Amount { get; set; }


        [JsonProperty("tax_rate_id")]
        public long TaxRateId { get; set; }


        [JsonProperty("ledger_account_id")]
        public long LedgerAccountId { get; set; }
    }

So, how can I get this "0" and "1" objects dynamic? Right now I get a result with objects that all have "0".

Thanks in advance!

This property:

[JsonProperty("details_attributes")]
public List<SalesInvoiceDetails> Details { get; set; }

Should be a Dictionary<string, SalesInvoiceDetailAttributes> instead of a List<SalesInvoiceDetails> .

Your SalesInvoiceDetails class is redundant for the structure you want and should be removed.

When you are adding to this Dictionary, that is where you will increment your key:

someObject.Details.Add("0", new SalesInvoiceDetailAttributes());
someObject.Details.Add("1", new SalesInvoiceDetailAttributes());
//etc

When it is serialized your json will look exactly how you want it.

I made a fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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