简体   繁体   English

如何将字符串解析为JObject忽略时区

[英]How parse String to JObject ignoring Time Zone

I have a String of json and need convert to JObject. 我有一个json字符串,需要转换为JObject。

Example: 例:

String result = ""{"totalSize":1,"done":true,"records":[{"attributes":{"type":"Municipio__c","url":"/services/data/v37.0/sobjects/Municipio__c/a0V2C000000gIgzUAE"},"Id":"a0V2C000000gIgzUAE","LastModifiedDate":"2017-08-01T18:12:04.000+0000"}]}";"

var json = JObject.Parse(result);

But in the moment of the convertion, the LastModifiedDate has changed the value to my Time Zone . 但是在转换的那一刻, LastModifiedDate已将值更改为我的时区

Like that: 像那样:

{{
  "totalSize": 1,
  "done": true,
  "records": [
    {
      "attributes": {
        "type": "Municipio__c",
        "url": "/services/data/v37.0/sobjects/Municipio__c/a0V2C000000gIgzUAE"
      },
      "Id": "a0V2C000000gIgzUAE",
      "LastModifiedDate": "2017-08-01T15:12:04-03:00"
    }
  ]
}}

The hour was changed: 18:12:04 (hh:MM:ss) to 15:12:04 (hh:MM:ss). 小时改变了: 181204 (hh:MM:ss)到15:12:04 (hh:MM:ss)。

Is there a way to ignore the Time Zone on the parse ? 有没有办法忽略解析时区?

You have to use JsonConvert with the appropriate DateTimeZoneHandling : 您必须使用JsonConvert和相应的DateTimeZoneHandling

var json = JsonConvert.DeserializeObject
                       ( result
                       , new JsonSerializerSettings()
                         { DateTimeZoneHandling = DateTimeZoneHandling.Utc
                         }
                       );

The time instances are the same. 时间实例是相同的。 18:12:04+00:00 is the same as 15:12:04-03:00 and 21:12:04+03:00 (the current offset in Greece). 15:12:04-03:00 18:12:04+00:0015:12:04-03:0015:12:04-03:00 21:12:04+03:00 (希腊目前的偏差)相同。

The default behaviour when parsing date values is to generate a local DateTime value, ie its Kind property will be DatTimeKind.Local`. 解析日期值时的默认行为是生成本地DateTime值,即其Kind属性将为DatTimeKind.Local`。 The other DateTimeKind values are UTC and Unspecified. 其他DateTimeKind值是UTC和Unspecified。

That doesn't mean that the value changed . 这并不意味着价值发生了变化 Local is a relative term though - without knowing the actual offset, it's easy to misinterpret it. Local是一个相对术语 - 不知道实际的偏移量,很容易误解它。

As Patrick Hofman explained, you can specify that UTC is used by setting the DateTimeZoneHandling setting. 正如Patrick Hofman所解释的那样,您可以通过设置DateTimeZoneHandling设置来指定使用UTC。

An even better way is to specify that DateTimeOffset will be used instead of DateTime by using the DateParseHandling = DateParseHandling.DateTimeOffset property. 一个更好的方法是指定DateTimeOffset将被用来代替DateTime通过使用DateParseHandling = DateParseHandling.DateTimeOffset财产。 This will return preserve the original timezone information : 这将返回保留原始时区信息:

var settings=new JsonSerializerSettings{ 
    DateParseHandling = DateParseHandling.DateTimeOffset
};
var json = JsonConvert.DeserializeObject( result, settings);

In this case, the value will be a DateTimeOffset with the original time and an offset of 00:00 . 在这种情况下,该值将是具有原始时间和偏移量00:00DateTimeOffset

I'm using dynamic here to reduce casting noise: 我在这里使用dynamic来减少铸造噪音:

var settings=new JsonSerializerSettings{ 
    DateParseHandling = DateParseHandling.DateTimeOffset
};

dynamic json = JsonConvert.DeserializeObject( result, settings);
var value=(DateTimeOffset)(json.records[0].LastModifiedDate.Value);

Console.WriteLine("{0:o}",value);

This will return : 这将返回:

2017-08-01T18:12:04.0000000+00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM