为什么Json.NET DeserializeObject将时区更改为本地时间? [英] Why does Json.NET DeserializeObject change the timezone to local time?
问题描述
我正在使用json.net反序列化DateTimeOffset
,但是它忽略了指定的时区并将datetime转换为本地偏移量.例如,给定
I'm using json.net to deserialize a DateTimeOffset
, but it is ignoring the specified timezone and converting the datetime to the local offset. For example, given
var content = @"{""startDateTime"":""2012-07-19T14:30:00+09:30""}";
使用以下方法反序列化时:
When deserialised using:
var jsonSerializerSettings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind };
var obj = JsonConvert.DeserializeObject(content, jsonSerializerSettings);
obj将包含一个包含DateTimeOffset
的属性,但该值将为2012-07-19T15:30:00+10:30
,即转换为本地时区而不是保留原始时区.
The obj will contain a property containing a DateTimeOffset
but the value will be 2012-07-19T15:30:00+10:30
i.e. converted to the local timezone instead of preserving the original timezone.
是否有一种方法可以按预期方式解析值,以使所得的DateTimeOffset
属性与提供的值匹配?
Is there a way to get the value to be parsed as expected so that the resulting DateTimeOffset
property will match the supplied value?
推荐答案
它似乎正在忽略DateParseHandling.DateTimeOffset
,并且正在使用DateParseHandling.DateTime
.我会在此处记录问题: https://github.com/JamesNK/Newtonsoft.Json
It seems to be ignoring DateParseHandling.DateTimeOffset
and is using DateParseHandling.DateTime
. I would log an issue here: https://github.com/JamesNK/Newtonsoft.Json
这篇关于为什么Json.NET DeserializeObject将时区更改为本地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!