-3

I am trying to loop through the list of JSON objects and convert them into indexed JSON.

Below is the code I have written:

private string ItemsAsJson(List<string> jsonItemList)
{
    string ItemAsJson = "";
    for (int i = 0; i < jsonItemList.Count; i++)
    {
        string index = i.ToString();
        ItemAsJson += "{ " + index + " : " + jsonItemList[i] + "},";
    }
    return ItemAsJson;
}

But I get the object as below here:

{
    0: {
        "item_type": "Batch",
        "item_id": "82",
        "bill_item_name": "A TO Z",
        "quantity": "1",
        "inventory_id": "82",
        "individual_price": "2.90",
        "batch_no": "",

How do I convert the 0 in the above text to string ("0")?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
  • 5
    You're writing out the literal JSON string, so put quotes around the 0? – Paul Walls Aug 21 '17 at 19:35
  • Your entire containing data type is a string. Where is the "object" you show in the second block coming from? is that coming directly from the `ItemsAsJson()` function? – AJ X. Aug 21 '17 at 19:36
  • 2
    An alternative is to do what is suggested in this answer. Let the people who develop JSON serializers define the correct JSON syntax. https://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp – AJ X. Aug 21 '17 at 19:38
  • 4
    Never form your json manually, Use a json parser like [Json.Net](https://www.newtonsoft.com/json) to do it for you – L.B Aug 21 '17 at 19:43
  • Do you have to write the JSON yourself? Json.NET will take care of the serialization details for you... – maccettura Aug 21 '17 at 19:43
  • @PaulWalls I tried putting quotes, its not working still. – Vivek Jeeva Kumar Aug 22 '17 at 05:00
  • @axlj Yes, the json object is coming from ItemsAsJson(). – Vivek Jeeva Kumar Aug 22 '17 at 05:01
  • 1
    @VivekJeevaKumar What's not working exactly? If you change your code to `ItemAsJson += "{ \"" + index + "\": " + jsonItemList[i] + "},";`, you'll get quotes around the index. If you are having a different problem, I'd recommend clarifying or asking a different question. – Paul Walls Aug 22 '17 at 09:52

1 Answers1

-1

The best answer would be to use a JSON parser library, like Json.NET, and not parse the JSON manually.

If you for some reason decide to proceed with your method, you can add quotes around the index number by escaping the quote in the string like \":

private string ItemsAsJson(List<string> jsonItemList)
{
    string ItemAsJson = "";
    for (int i = 0; i < jsonItemList.Count; i++)
    {
        string index = i.ToString();
        ItemAsJson += "{ \"" + index + "\": " + jsonItemList[i] + "},";
    }
    return ItemAsJson;
}

On a side note, if you are building a string like this, I would recommend using StringBuilder instead of +=:

private string ItemsAsJson(List<string> jsonItemList)
{
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < jsonItemList.Count; i++)
    {
        builder.Append("{ \"");
        builder.Append(i);
        builder.Append("\":");
        builder.Append(jsonItemList[i]);
        builder.Append("},");
    }
    return builder.ToString();
}
Tot Zam
  • 8,406
  • 10
  • 51
  • 76