-1

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"
}]
TheDizzle
  • 1,534
  • 5
  • 33
  • 76
  • Are you using a serializer, e.g. one of the ones described in [this answer](http://stackoverflow.com/q/6201529/3744182), or are you trying to manually create JSON via string formatting? – dbc Feb 22 '17 at 16:27
  • @dbc i just `return Json(info, JsonRequestBehavior.AllowGet);` – TheDizzle Feb 22 '17 at 16:28
  • In that case I'd suggest you expand your question to an [mcve] and tag it fully ([tag:asp.net-mvc] or [tag:asp.net-web-api] or whatever). – dbc Feb 22 '17 at 16:36

4 Answers4

1

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.

Dispersia
  • 1,436
  • 9
  • 23
1

If you make those string as object.

public class Invoice{
 public string InvoiceNumber{get;set;}
 public string GrandTotal{get;set;}
 public string Date{get;set;}
}


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

    foreach (var invoice in theInvoices)
    {
        invoicesList.Add(new Invoice(){InvoiceNumber=invoice.InvoiceNumber,          
              GrandTotal= invoice.GrandTotal, 
             Date=FieldTranslation.ToShortDate(invoice.Date.ToString())});
    }

Then there is a package called NewtonSoftJson which allows you to convert from Collection.

Go to package manager console (Click on view menu in visual studio-> Other windows -> Package Manager console)

Install-Package Newtonsoft.Json

C# code

var invoiceListString=JsonConvert.SerializeObject(invoicesList);

Roath So
  • 141
  • 6
0

I will recommend to use NewtonSoft.Json.

public class Invoice
{
    public string InvoiceNumber { get; set; }
    public DateTime InvoiceDate { get; set; }
    public string InvoiceTotal { get; set; }
}

public void PrintJson()
{
     List<Invoice> InvoiceList = new List<Invoice>();

     var outputObject = new { InvoiceList };

     InvoiceList.Add(new Invoice { InvoiceNumber = "SALES0000001", InvoiceDate = DateTime.UtcNow, InvoiceTotal = String.Format("{0:c}", "90000") });
     InvoiceList.Add(new Invoice { InvoiceNumber = "SALES0000002", InvoiceDate = DateTime.UtcNow, InvoiceTotal = String.Format("{0:c}", "60000") });

     var output1 = JsonConvert.SerializeObject(outputObject);
     var output2 = JsonConvert.SerializeObject(InvoiceList);
}

Output1

{"InvoiceList":[{"InvoiceNumber":"SALES0000001","InvoiceDate":"2017-02-22T16:46:31.6933956Z","InvoiceTotal":"90000"},{"InvoiceNumber":"SALES0000002","InvoiceDate":"2017-02-22T16:46:31.6933956Z","InvoiceTotal":"60000"}]}

Output2

[{"InvoiceNumber":"SALES0000001","InvoiceDate":"2017-02-22T16:46:31.6933956Z","InvoiceTotal":"90000"},{"InvoiceNumber":"SALES0000002","InvoiceDate":"2017-02-22T16:46:31.6933956Z","InvoiceTotal":"60000"}]

Please notice the Json in your question is not valid.

Liu
  • 970
  • 7
  • 19
0

As pointed by Dispersia, your list is one big string list and, therefore, the json serializer behaves as requested -- turns this big string list into a big json string array.

I haven't got VS at hand to test the code below but try turning your invoice list into something like:

List<Tuple<String, String, String>> invoiceList = new List<>();

and then add tuples accordingly:

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

Would that help?

Jacek
  • 1,048
  • 15
  • 21