javascript - What is JSON.NET for? MVC already has a way to convert objects to json -
i looked @ json.net site not getting it... have code ..
string[] invalidfiles = new string[] { "one.xls", "two.xls", "three.xls" }; return json(new { status = "ok", invalidfiles = invalidfiles }); that turns objects json... looked @ json.net , seems same thing ? missing ? isa simple example of json.net can can
protected internal jsonresult json(object data); ??
for web api in mvc 4 default serializer json.net library, when returning jsonresult in mvc 4 standard controllers default serializer used javascriptserializer. can create custom jsonresult , override json method in standard controllers use json.net library default.
to use json.net can first create custom result:
public class jsonnetresult : jsonresult { public override void executeresult(controllercontext context) { if (context == null) { throw new argumentnullexception("controller context"); } httpresponsebase response = context.httpcontext.response; if (contentencoding != null) { response.contentencoding = contentencoding; } var jsondata = jsonconvert.serializeobject(data); response.contenttype = !string.isnullorempty(contenttype) ? contenttype : "application/json"; response.write(jsondata); } } then override json method in base controller method , return json doing:
protected override jsonresult json(object data, string contenttype, encoding contentencoding, jsonrequestbehavior behavior) { return new jsonnetresult() { data = data, contenttype = contenttype, contentencoding = contentencoding }; }
Comments
Post a Comment