[英]Datetime in json string is getting converted to localtime
Why is the datetime+offset is getting converted to localtime+offset when convert the json string to Jobject. 将json字符串转换为Jobject时,为什么datetime + offset被转换为localtime + offset。
Here is the code. 这是代码。
string dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";
JObject json = JObject.Parse(dataValue);
Console.Write(json.ToString());
Output: 输出:
{
"Time": "2016-07-15T17:33:41+05:30"
}
Expected Output: 预期产量:
{
"Time": "2016-07-15T20:03:41+08:00"
}
By default it will be using the local time, but you can override the default settings: 默认情况下,它将使用当地时间,但是您可以覆盖默认设置:
var dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";
var jsonSerializerSettings = new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
var json = JsonConvert.DeserializeObject<JObject>(dataValue, jsonSerializerSettings);
If you don't care about the date being converted to a DateTime type, you can tell Json.NET to just ignore dates and parse the value as a string : 如果您不关心将日期转换为DateTime类型,可以告诉Json.NET忽略日期并将值解析为字符串 :
var dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";
var jsonSerializerSettings = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
};
var json = JsonConvert.DeserializeObject<JObject>(dataValue, jsonSerializerSettings)
In such a way it will stay exactly as your input. 这样,它将完全保持您的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.