ASP.NET MVC Controller.Json日期时间序列化VS NewtonSoft的Json序列化的DateTime [英] ASP.NET MVC Controller.Json DateTime Serialization vs NewtonSoft Json DateTime Serialization
问题描述
当我回到使用包含的DateTime属性对象
When I return object that contains DateTime property using
return Json(value);
在客户端我收到
"/Date(1336618438854)/"
如果我使用返回相同的值
If i return the same value using
return Json(JsonConvert.SerializeObject(value));
则返回序列化值(和序列化对象一起)是时区的意识到:
then the returned serialized value (together with serialized object) is time zone aware:
"/Date(1336618438854-0400)/"
有什么办法来获得一致的日期时间的结果,而不无双重序列化?我某处读到MS将包括Newtonsoft JSON到MVC?
Is there any way to get consistent DateTime result without without double serialization? I read somewhere that MS will include Newtonsoft JSON into MVC?
推荐答案
我终于想通了做什么。结果
我将切换我的项目ISO 8601 DateTime格式。序列化只是通过JsonConverter属性的对象上装饰的datetime财产JSON.net很好做。
I finally figured out what to do.
I will switch my project to ISO 8601 DateTime format. Serialization is done nicely with JSON.net, just by decorating the datetime property on the object with JsonConverter attribute.
public class ComplexObject
{
[JsonProperty]
public string ModifiedBy { get; set; }
[JsonProperty]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime Modified { get; set; }
...
}
要返回序列化对象到客户端AJAX调用我可以做的:
To return serialized object to the client ajax call I can do:
return Json(JsonConvert.SerializeObject(complexObjectInstance));
和客户端上的:
jsObject = JSON.parse(result)
现在我想这将是可能简单覆盖默认的ASP.NET MVC默认JSON序列化给我们Newtonsoft JSON.net ISO 8601的序列化,是的原则应该是类似这样的主题:<一href=\"http://stackoverflow.com/questions/6883204/change-default-json-serializer-used-in-asp-mvc3\">Change默认JSON序列用于ASP MVC3 。
Now I am thinking it would be probably simple to override default ASP.NET MVC default JSON serializer to us Newtonsoft JSON.net ISO 8601 serialization, and yes principle should be similar to this thread: Change Default JSON Serializer Used In ASP MVC3.
这篇关于ASP.NET MVC Controller.Json日期时间序列化VS NewtonSoft的Json序列化的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!