Convert a list to JSON from a table

advertisements

I am trying to convert a list to a json string. The json is generating fine, but when I try to generate an array within the JSON it won't format properly.

My list is generated

List<string> invoicesList = new List<string>();

        foreach (var invoice in theInvoices)
        {
            invoicesList.Add(invoice.InvoiceNumber);
            invoicesList.Add(String.Format("{0:c}", invoice.GrandTotal));
            invoicesList.Add(FieldTranslation.ToShortDate(invoice.Date.ToString()));
        }

Then I add it to the JSON

var info = new MobileAPIHelper.ClientPayrollInfo
        {
            GrossWages = String.Format("{0:c}", GrossWages),
            InvoiceTotal = String.Format("{0:c}", invoiceTotal),
            InvoiceDate = FieldTranslation.ToShortDate(invoiceDate.ToString()),
            InvoiceList = invoicesList.ToArray()

        };

The output ends up just being a long JSON string with everything from the list

"InvoiceList":["SALES000000000006","$9,300.00","4/11/2016","SALES000000000008","$4,650.00","12/22/2015"]

What I can't figure out is how to get the list / json to format that invoicelist like so:

"InvoiceList":[{
"SALES000000000006","$9,300.00","4/11/2016"
},{
"SALES000000000008","$4,650.00","12/22/2015"
}]


invoicesList is not a list of an object that contains those values, it's a list of strings. You need to make a class that acts as a container for

invoice.InvoiceNumber;
String.Format("{0:c}", invoice.GrandTotal);
invoice.Date.ToString());

these fields. Make invoicesList a list of that class, then parse it to json. You're adding raw strings.