Returning unescaped Json in MVC with Json.Net
2014-06-28 02:45
399 查看
http://stackoverflow.com/questions/7382265/returning-unescaped-json-in-mvc-with-json-net
1.
The object is already serialized by Json.NET, and when you pass it to Json() it gets encoded twice. If you must use Json.NET instead of the built in encoder, then the ideal way to handle this would be to create a custom ActionResult accepts the object and calls
Json.net internally to serialize the object and return it as an application/json result.
EDIT
This code is for the solution mentioned above. It's untested, but should work.
and in your controller just do:
2.
You are Jsoning it twice, the
is json serializing your already converted string. If you want to use JsonConvert then write that directly to the response stream.
1.
The object is already serialized by Json.NET, and when you pass it to Json() it gets encoded twice. If you must use Json.NET instead of the built in encoder, then the ideal way to handle this would be to create a custom ActionResult accepts the object and calls
Json.net internally to serialize the object and return it as an application/json result.
EDIT
This code is for the solution mentioned above. It's untested, but should work.
public class JsonDotNetResult : ActionResult { private object _obj { get; set; } public JsonDotNetResult(object obj) { _obj = obj; } public override void ExecuteResult(ControllerContext context) { context.HttpContext.Response.AddHeader("content-type", "application/json"); context.HttpContext.Response.Write(JsonConvert.SerializeObject(_obj)); } }
and in your controller just do:
return new JsonDotNetResult(result);
2.
You are Jsoning it twice, the
Jsonmethod
is json serializing your already converted string. If you want to use JsonConvert then write that directly to the response stream.
相关文章推荐
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- Replace JSON.NET with ServiceStack.Text in ASP.NET Web API
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?
- Configuring Autofac to work with the ASP.NET Identity Framework in MVC 5
- How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?
- Redirect and Post JSON object in ASP.NET MVC
- ASP.NET MVC - Redirect-and-Post-JSON-Object-in-ASP-NET-MVC
- Multiple Models in a View in ASP.NET MVC 4 / MVC 5 (with source code)
- ASP.NET MVC – Create easy REST API with JSON and XML(转)
- Working with Roles in ASP.NET MVC 4+
- Active Directory Authentication in ASP.NET MVC 5 with Forms Authentication and Group-Based Authorization
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- Developing a plugin framework in ASP.NET MVC with medium trust
- Replace JSON.NET with Jil JSON serializer in ASP.NET Web API
- Replacing MVC JavascriptSerializer with JSON.NET J
- jQuery UI Accordion in ASP.NET MVC - feed with data from database
- Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at charac
- ASP.NET MVC #01, Create a Movie Database Application in 15 Minutes with ASP.NET MVC
- Upload files in ASP.NET MVC with JavaScript and C#