CmdrTallen
CmdrTallen

Reputation: 2282

How do I produce a JSON string array?

Trying to get a JSON output to work with jqGrid 'userdata' option.

The example given in the jqGrid PDF is to produce a JSON result like this;

{   total: "xxx",  
    page: "yyy", 
    records: "zzz",  
    userdata: {totalinvoice:240.00, tax:40.00}, 
    rows : [ 
        {id:"1", cell:["cell11", "cell12", "cell13"]}, 
        {id:"2", cell:["cell21", "cell22", "cell23"]}, 
    ] 
}

I have tried to make the userdata portion like this (with no luck);

 var jsonData = new 
 { ...
   userdata = new string[] {"totalinvoice:240.00", "tax:40.00" }
   ...}
 return Json(jsonData);

But that comes back in the JSON results looking like this;

userdata":["totalinvoice:240.00","tax:40.00"]

I presume this is something trivial, but I cant seem to figure out how to get the System.Web.JSON.JsonResult call to return the value as expected by jqGrid.

Upvotes: 0

Views: 1354

Answers (1)

womp
womp

Reputation: 116977

Try:

var jsonData = new 
 { ...
   userdata = new { totalinvoice = 240.00, tax = 40.00 }
   ...}
 return Json(jsonData);

In JSON notation, { } is for objects, [ ] is for arrays. If it's expecting something in curly braces, you should serialize an object with those properties.

Upvotes: 4

Related Questions