2

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);
4

2 に答える 2

4

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.

于 2012-05-28T10:46:40.847 に答える
1

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

于 2012-05-28T10:50:43.097 に答える