我創建了一個新的MVC4應用程序,默認情況下牛頓JSON添加到包。Newtonsoft JSON
我讀這是序列化和反序列化JSON有用。這是否只是它?
默認情況下,我們可以使用JSONResult在MVC發送JSON。並在JQuery中使用Stringify,我可以在C#中作爲一個類接收。
我知道應該有一些原因,他們加入牛頓JSON。
由於我是新來的MVC和出發的新項目想知道的一些見解,其中序列化/反序列化去?
感謝
我創建了一個新的MVC4應用程序,默認情況下牛頓JSON添加到包。Newtonsoft JSON
我讀這是序列化和反序列化JSON有用。這是否只是它?
默認情況下,我們可以使用JSONResult在MVC發送JSON。並在JQuery中使用Stringify,我可以在C#中作爲一個類接收。
我知道應該有一些原因,他們加入牛頓JSON。
由於我是新來的MVC和出發的新項目想知道的一些見解,其中序列化/反序列化去?
感謝
他們補充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
}
因此,這增加了功能,如果我使用API其他我i可以與其他選項一起使用? – user2067567
如果你正在做一個MVC項目,那麼你仍然可以使用MVC 3中的知識(例如'return Json(the_object)')。如果它是一個API,那麼不需要自行序列化。 –
前一段時間,但我有同樣的問題,但得出了不同的結論。 Newtonsoft.json包含在默認MVC中,因爲WebGrease依賴於它,而不是因爲WebApi可能需要它。 – rism
使用JsonResult並返回Json(yourObject)
如果您正在執行GET操作,則可以使用Json(yourObject, JsonRequestBehavior.AllowGet)
。
如果要反序列化的Json與牛頓Json.NET,檢查出http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx
如果您的項目只是一個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
。
這是關於性能和靈活性。有關性能,請參閱http://james.newtonking.com/archive/2012/01/23/json-net-4-0-release-6-serialization-performance.aspx –