0

i have searched but nothing that help me i have found. I have this JSON:

{
"id": 1,
"customer_ID": 1,
  "address": {
    "id": 1,
    "description": "primary address"
}
},{
    "id": 2,
    "customer_ID": 2,
    "address": {
        "id": 2,
        "description": "primary address"
    }
}

And if i try to add to my DataTables i have an error:

Requested unknown parameter 'id' for row 0, column 0

But, if i convert my JSON to this, the table is filled:

{"data": [
  {
    "id": 1,
    "customer_ID": 1,
    "address": 
    {
       "id": 1,
       "description": "primary address"
    }
  },{
        "id": 2,
        "customer_ID": 2,
        "address": 
        {
            "id": 2,
            "description": "primary address"
        }
    }]}

This is my Javascript code:

$("#tblCustomerAddress").DataTable({
                    data: result,
                    destroy: true,
                    columns: [
                        { "data": 'id' },
                        { "data": 'address.description' },
                        { "data": 'customer_ID' }
                    ]
                });

The "result" field is returned by my C# code:

public async Task<IActionResult> OnPostSaveAddressAsync(string GUID, string Address)        
{
    ...

    JsonResult Json = new JsonResult(wItem.Address_List);

    return Json;
}

My question is: how can i add this {data: []} to my JSON without manipulate with a stringbuilder? There are no default function that cover this?

MrTex
  • 239
  • 4
  • 18
  • 1
    Your first JSON sample is not well-formed. It has an extra comma on line 6, but more importantly, it has two comma-separated root objects that lack outer array brackets `[` and `[`. This violates the [JSON standard](https://json.org/). Upload to https://jsonformatter.curiousconcept.com/ and you will see the errors. How did you manage to create it? [tag:asp.net-core] should never return such malformed JSON, – dbc Nov 15 '19 at 09:54
  • (Your second JSON sample is less malformed; you need to quote the property name `"data"` and remove extra commas after the `"description"` property values. Might this be a typo in your question?) – dbc Nov 15 '19 at 09:57
  • the extra coma is my copy and paste error(i've deleted other information not necessary). the problem is that JSON is created with my c# code(JsonResult Json = new JsonResult(wItem.Address_List)), starting from my Address_List class. – MrTex Nov 15 '19 at 10:06
  • It's more likely we can help you if you provide a [mcve]. But perhaps you could return an anonymous object wrapping your array, e.g. `new JsonResult(new { data = wItem.Address_List });`, as is shown in [How to add a root node to a JSON in C# using Json.NET?](https://stackoverflow.com/a/37596989). – dbc Nov 15 '19 at 10:46

0 Answers0