how to get Json.net serialize DateTime similar to JavaScriptSerializer?

社会主义新天地 提交于 2021-02-16 14:36:10

问题


My code base had been using JavaScriptSerializer for long time, now I am planning to migrate to Json.Net. I saw that DateTime Serializing on both work differently.

Console.WriteLine(new JavaScriptSerializer().Serialize(DateTimeOffset.UtcNow));

Output:"\/Date(1403629024695)\/"

Console.WriteLine(JsonConvert.SerializeObject(DateTimeOffset.UtcNow));

Output: "2014-06-24T16:57:04.6954145+00:00"

Is there a way to get Json.net to serialize similar to JavaScriptSerializer?


回答1:


You can use DateFormatHandling.MicrosoftDateFormat

Console.WriteLine(JsonConvert.SerializeObject(DateTimeOffset.UtcNow, 
                                              new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }));


来源:https://stackoverflow.com/questions/24392356/how-to-get-json-net-serialize-datetime-similar-to-javascriptserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!