强制 Json 序列化器生成特定的日期时间格式 (yyyy-mm-ddThh:mm:ss.msmsmsZ)
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"
?
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));
Produces:
{"accountId":12345,"timeStamp":"\/Date(1268420981135)\/","userType":"theUserType"}
How can I present the date as:
"timestamp":"2010-02-15T23:53:35.963Z"
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要制作一个
JavaScriptConverter< /code>
类并使用
RegisterConverters
方法。You need to make a
JavaScriptConverter
class and register it using theRegisterConverters
method.即使您实现了 JavaScriptConverter,您也必须将字符串包装在对象中。幸运的是,这里有一个解决办法:
http://blog .calyptus.eu/seb/2011/12/custom-datetime-json-serialization/
Even if you implement a JavaScriptConverter you would have to wrap the string in an object. Fortunately there's a hack around it described here:
http://blog.calyptus.eu/seb/2011/12/custom-datetime-json-serialization/
我建议您(以及其他遇到此问题的人)切换到 ServiceStack.Text 库 - 集成时间大约为 30 秒,您将解决许多其他问题。看看这个发布的问题&我回答:
ASP.NET MVC Json DateTime Serialization conversion to UTC
I recommend that you (and everybody else with this problem) just switch to ServiceStack.Text library - it's like 30 seconds to integrate and you'll solve bunch of other problems. Take a look at this question posted & answered by me:
ASP.NET MVC Json DateTime Serialization conversion to UTC