2013-04-01 60 views
6

我创建了一个新的MVC4应用程序,默认情况下牛顿JSON添加到包。Newtonsoft JSON

我读这是序列化和反序列化JSON有用。这是否只是它?

默认情况下,我们可以使用JSONResult在MVC发送JSON。并在JQuery中使用Stringify,我可以在C#中作为一个类接收。

我知道应该有一些原因,他们加入牛顿JSON。

由于我是新来的MVC和出发的新项目想知道的一些见解,其中序列化/反序列化去?

感谢

+0

这是关于性能和灵活性。有关性能,请参阅http://james.newtonking.com/archive/2012/01/23/json-net-4-0-release-6-serialization-performance.aspx –

回答

6

他们补充Newtonsoft让您的WebAPI控制器能神奇系列化你返回的对象。在MVC 3,我们用来返回我们的对象,像这样:

public ActionResult GetPerson(int id) 
{ 
    var person = _personRepo.Get(id); 
    return Json(person); 
} 

的Web API项目可以返回的人,将被系列化您:

public Person GetPerson(int id) 
{ 
    var person = _personRepo.Get(id); 
    return person 
} 
+0

因此,这增加了功能,如果我使用API​​其他我i可以与其他选项一起使用? – user2067567

+0

如果你正在做一个MVC项目,那么你仍然可以使用MVC 3中的知识(例如'return Json(the_object)')。如果它是一个API,那么不需要自行序列化。 –

+0

前一段时间,但我有同样的问题,但得出了不同的结论。 Newtonsoft.json包含在默认MVC中,因为WebGrease依赖于它,而不是因为WebApi可能需要它。 – rism

0

如果您的项目只是一个MVC项目没有的WebAPI,然后Newtonsoft.Json没有加入返回JsonResults由MVC返回JsonResult使用JavaScriptSerializer如下:

public override void ExecuteResult(ControllerContext context) 
     { 
      if (context == null) 
      { 
       throw new ArgumentNullException("context"); 
      } 
      if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && 
       String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) 
      { 
       throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed); 
      } 

      HttpResponseBase response = context.HttpContext.Response; 

      if (!String.IsNullOrEmpty(ContentType)) 
      { 
       response.ContentType = ContentType; 
      } 
      else 
      { 
       response.ContentType = "application/json"; 
      } 
      if (ContentEncoding != null) 
      { 
       response.ContentEncoding = ContentEncoding; 
      } 
      if (Data != null) 
      { 
       JavaScriptSerializer serializer = new JavaScriptSerializer(); 
       if (MaxJsonLength.HasValue) 
       { 
        serializer.MaxJsonLength = MaxJsonLength.Value; 
       } 
       if (RecursionLimit.HasValue) 
       { 
        serializer.RecursionLimit = RecursionLimit.Value; 
       } 
       response.Write(serializer.Serialize(Data)); 
      } 
     } 

在这种情况下,它加入,因为WebGrease上有一个依赖。 MVC在System.Web.Optimization提供的捆绑和缩小服务依赖于WebGrease

所以默认MVC应用程序没有的WebAPI将已安装了捆绑和缩小服务没有的WebAPI Newtonsoft.Json

要明确通过的WebAPI在System.Web.Http返回并使用Newtonsoft.Json为它的序列化,如下JsonResult

using Newtonsoft.Json; 
using System; 
using System.IO; 
using System.Net; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web.Http; 

namespace System.Web.Http.Results 
{ 
    /// <summary> 
    /// Represents an action result that returns an <see cref="F:System.Net.HttpStatusCode.OK"/> response with JSON data. 
    /// </summary> 
    /// <typeparam name="T">The type of content in the entity body.</typeparam> 
    public class JsonResult<T> : IHttpActionResult 

Newtonsoft.Json不包括在非的WebAPI,默认MVC项目,以防万一你可能决定使用一些WebApi,它在那里,因为,如上所述,WebGrease需要它。不知道他们在vNext中做了些什么,可能Newtonsoft.Json