为什么将json字符串转换为Jobject时,datetime+offs被转换为localtime+offs。
这是代码。
string dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";
JObject json = JObject.Parse(dataValue);
Console.Write(json.ToString());
输出:
{
"Time": "2016-07-15T17:33:41+05:30"
}
预期输出:
{
"Time": "2016-07-15T20:03:41+08:00"
}
默认情况下,它将使用本地时间,但您可以覆盖默认设置:
var dataValue = @"{""Time"":""2016-07-15T20:03:41+08:00""}";
var jsonSerializerSettings = new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
var json = JsonConvert.DeserializeObject<JObject>(dataValue, jsonSerializerSettings);
如果您不关心将日期转换为 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)
这样,它将完全保持您的输入。