强制 Json 序列化器生成特定的日期时间格式 (yyyy-mm-ddThh:mm:ss.msmsmsZ)

发布于 2024-08-25 03:21:54 字数 527 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

々眼睛长脚气 2024-09-01 03:21:54

您需要制作一个 JavaScriptConverter< /code>类并使用 RegisterConverters 方法。

You need to make a JavaScriptConverter class and register it using the RegisterConverters method.

沫尐诺 2024-09-01 03:21:54

即使您实现了 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/

天涯沦落人 2024-09-01 03:21:54

我建议您(以及其他遇到此问题的人)切换到 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文