MyClass theSession = new MyClass() {
    accountId = 12345,
    timeStamp = DateTime.Now,
    userType = "theUserType"
};

System.Web.Script.Serialization.JavaScriptSerializer Json = new System.Web.Script.Serialization.JavaScriptSerializer();
Response.Write(Json.Serialize(theSession));

生产:

{"accountId":12345,"timeStamp":"\/Date(1268420981135)\/","userType":"theUserType"}

我如何可以本的日期为:

"timestamp":"2010-02-15T23:53:35.963Z"

?

有帮助吗?

解决方案

即使你实现一个JavaScriptConverter你就必须包装在字符串中的对象。幸运的是围绕在这里描述的黑客:

http://blog.calyptus.eu/seb / 2011/12 /定制日期时间-JSON序列化/

其他提示

你需要做一个 JavaScriptConverter 类和登记,使用的 RegisterConverters 法。

我建议你(和其他人有这个问题),只需切换到ServiceStack.Text库 - 就像30秒的整合,你就会解决一堆其他问题。看看这个问题,发布和回答了我:

ASP.NET MVC的Json日期时间序列化转化为UTC

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top