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?

4
  • 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. Upload to jsonformatter.curiousconcept.com and you will see the errors. How did you manage to create it? asp.net-core should never return such malformed JSON, Commented Nov 15, 2019 at 9: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?) Commented Nov 15, 2019 at 9: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. Commented Nov 15, 2019 at 10:06
  • It's more likely we can help you if you provide a minimal reproducible example. 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?. Commented Nov 15, 2019 at 10:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.