Reputation: 21226
With JSON.NET I can serialize a generic list into a JSON string:
return Json(new { success = true, data = JsonConvert.SerializeObject(units) });
but how can I serialize a generic list into JSON objects. This would have the advantage that I do not need this on client side:
var jsonData = $.parseJSON(units);
Upvotes: 2
Views: 9613
Reputation: 2123
Check out this link.
I personally use json2.js, and JSON.parse/stringify to cnovert strings to ojbects and vice versa
p.s its stil done on the client side, but its just another line of code to add
Upvotes: 1
Reputation: 65166
Don't serialize a part of the object, serialize the entire object:
return Content(JsonConvert.SerializeObject(new { success = true, data = units }), "text/javascript");
The built-in Json
method is hard-coded to return a result that uses the built-in (somewhat limited) .NET JavaScript serializer. If you want to get something as easy to use as that, add your own "Json" method into your base controller class that does the same thing with Json.NET.
Upvotes: 4