本文介绍了你能告诉JSON.Net即使未指定的序列化的DateTime UTC作为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的数据库中的日期存储为UTC。但是,当我retreieve他们瓦特/实体框架就出来类型不明。
Dates in my database are stored as Utc. But when I retreieve them w/ the entity framework they come out as type unspecified.
在JSON.Net连载他们,他们不UTC格式。有没有办法告诉JSON.Net序列化DateTime是否为UTC即使它们的类型没有被指定为UTC?
When JSON.Net serializes them they are not in Utc format. Is there a way to tell JSON.Net to serialize DateTimes as Utc even if their type is not specified as Utc?
推荐答案
设置 DateTimeZoneHandling
在 JsonSerializerSettings
到 UTC
。这将所有日期UTC转换序列化之前。
Set DateTimeZoneHandling
on JsonSerializerSettings
to Utc
. That will convert all dates to UTC before serializing them.
public void SerializeObjectDateTimeZoneHandling()
{
string json = JsonConvert.SerializeObject(
new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified),
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
Assert.AreEqual(@"""2000-01-01T01:01:01Z""", json);
}
文档:
这篇关于你能告诉JSON.Net即使未指定的序列化的DateTime UTC作为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!