Newtonsoft JSON
我创建了一个新的MVC4应用程序,默认情况下,Newton JSON添加到包中 我读到它对于序列化和反序列化JSON很有用。这就是它的全部功能吗 默认情况下,我们可以使用JSONResult在MVC中发送JSON。在JQuery中使用Stringify,我可以在C#中作为类接收 我知道他们添加Newton JSON应该是有原因的 由于我是MVC新手,刚开始新的项目,我想知道哪些序列化/反序列化方法值得关注Newtonsoft JSON,json,asp.net-mvc-4,Json,Asp.net Mvc 4,我创建了一个新的MVC4应用程序,默认情况下,Newton JSON添加到包中 我读到它对于序列化和反序列化JSON很有用。这就是它的全部功能吗 默认情况下,我们可以使用JSONResult在MVC中发送JSON。在JQuery中使用Stringify,我可以在C#中作为类接收 我知道他们添加Newton JSON应该是有原因的 由于我是MVC新手,刚开始新的项目,我想知道哪些序列化/反序列化方法值得关注 感谢他们添加了Newtonsoft,这样您的WebAPI控制器就可以神奇地序列化返回的对象
感谢他们添加了Newtonsoft,这样您的WebAPI控制器就可以神奇地序列化返回的对象。在MVC 3中,我们通常像这样返回对象:
public ActionResult GetPerson(int id)
{
var person = _personRepo.Get(id);
return Json(person);
}
在Web API项目中,您可以返回person,它将为您序列化:
public Person GetPerson(int id)
{
var person = _personRepo.Get(id);
return person
}
在post操作中使用JsonResult并返回
Json(yourObject)
,或者在执行GET操作时返回Json(yourObject,JsonRequestBehavior.AllowGet)
如果您想使用Newton Json.NET反序列化Json,请查看如果您的项目只是一个没有WebApi的MVC项目,则不会添加
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));
}
}
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
在本例中,添加它是因为webfleep
依赖于它。MVC在System.Web.Optimization
中提供的捆绑和缩小服务依赖于webfeel
因此,没有WebApi的默认MVC应用程序将安装Newtonsoft.Json
,用于捆绑和缩小服务,而不是WebApi
为了明确起见,WebApi在System.Web.Http
中返回的JsonResult
使用Newtonsoft.Json
进行序列化,如下所示:
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));
}
}
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;
使用制度;
使用System.IO;
Net系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Web.Http;
命名空间System.Web.Http.Results
{
///
///表示返回带有JSON数据的响应的操作结果。
///
///实体主体中的内容类型。
公共类JsonResult:IHttpActionResult
但是,
Newtonsoft.Json
不包括在非WebApi的默认MVC项目中,以防您决定使用某些WebApi,因为如上所述,webfeel
需要它。不确定他们在vNext中做什么,可能是关于性能和灵活性的如果我使用API,s会添加功能,否则我可以使用其他选项?如果你正在进行MVC项目,那么你仍然可以使用MVC 3中的知识(例如返回Json(the_对象)
)。如果它是一个API,则无需自行序列化。不久前,我也有同样的问题,但得出了不同的结论。默认MVC中包含Newtonsoft.json,是因为WebFeel依赖它,而不是因为WebApi可能需要它。