问题
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