简体   繁体   English

如何使用Newtonsoft.JSON JObject获得一致的日期时间值

[英]How to get consistent date time value with Newtonsoft.JSON JObject

I have a ASP.Net Core service that uses Newtonsoft.Json library for working with JSON data. 我有一个ASP.Net Core服务,该服务使用Newtonsoft.Json库处理JSON数据。 The sample JSON input has a string that holds a date value in ISO8601 format. 样本JSON输入包含一个字符串,其中包含ISO8601格式的日期值。 However I am observing different date time value being returned for an equivalent date. 但是,我观察到在相同的日期返回不同的日期时间值。 Here is the sample code - 这是示例代码-

    var jsonString = @"{
        ""data"": {
            ""name"": ""John Doe"",
            ""dateOfBirth"": ""1990-05-25T15:54:49.119+00:00""
        }
    }";

    var jsonObj = JObject.Parse(jsonString);
    var person = jsonObj.ToObject<Person>();

    DateTime dateOfBirth = DateTime.Parse(person.Data.DateOfBirth);
    if (dateOfBirth.Kind != DateTimeKind.Utc)
    {
        dateOfBirth = dateOfBirth.ToUniversalTime();
    }
    Console.WriteLine("Date of birth is " + dateOfBirth.ToString("o"));

The Person class is like this - Person类是这样的-

class Person
{
    public PersonalData Data;
}

class PersonalData
{
    public  string Name { get; set; }
    public string DateOfBirth { get; set; }
}

If I provide ""dateOfBirth"": ""1990-05-25T15:54:49.119+00:00 "", the output is - 如果我提供""dateOfBirth"": ""1990-05-25T15:54:49.119+00:00 ”“,则输出为-

Date of birth is 1990-05-25T15:54:49.0000000Z

If I provide ""dateOfBirth"": ""1990-05-25T15:54:49.119Z"" , the output is - 如果我提供""dateOfBirth"": ""1990-05-25T15:54:49.119Z"" ,则输出为-

Date of birth is 1990-05-25T10:24:49.0000000Z

As it can be seen, the output is different where as it should have been same. 可以看出,输出应该在相同的地方是不同的。 The callers can set any date time string in ISO8601 format. 呼叫者可以设置ISO8601格式的任何日期时间字符串。

Is there any way where this can be handled consistently? 有什么方法可以始终如一地处理吗?

This is because DateTime.Parse() takes into account the system time zone (of the computer you run this code) 这是因为DateTime.Parse()考虑了系统时区(运行此代码的计算机的时区)

The solution is here 解决方案在这里

   var jsonString = @"{
        ""data"": {
            ""name"": ""John Doe"",
            ""dateOfBirth"": ""1990-05-25T15:54:49.119+00:00""
        }
    }";
var jsonObj = JObject.Parse(jsonString);
var person = jsonObj.ToObject<Person>();

var dateTimeOffset = DateTimeOffset.Parse(person.Data.DateOfBirth, CultureInfo.InvariantCulture);
DateTime dateOfBirth = dateTimeOffset.UtcDateTime;
Console.WriteLine("Date of birth is " + dateOfBirth.ToString("o"));

If you use DateTimeOffset it should fix your issue. 如果使用DateTimeOffset,它应该可以解决您的问题。 Rather than trying to rewrite information that already exists I would point you to a couple of existing resources: 与其尝试重写已经存在的信息,不如指出一些现有资源:

These uses for DateTimeOffset values are much more common than those for DateTime values. 这些对DateTimeOffset值的用法比对DateTime值的用法更为普遍。 As a result, DateTimeOffset should be considered the default date and time type for application development. 因此,应将DateTimeOffset视为应用程序开发的默认日期和时间类型。

From MSDN Development Guide MSDN开发指南

DateTime vs DateTimeOffset DateTime vs DateTimeOffset

I have tried using JsonConvert.DeserializeObject(jsonString) and got the expected output. 我尝试使用JsonConvert.DeserializeObject(jsonString)并获得了预期的输出。 Please check this fiddle 请检查这个小提琴

https://dotnetfiddle.net/uuZvab https://dotnetfiddle.net/uuZvab

Person person = JsonConvert.DeserializeObject<Person>(jsonString);
                DateTime dateOfBirth = DateTime.Parse(person.Data.DateOfBirth);
                if (dateOfBirth.Kind != DateTimeKind.Utc)
                {
                    dateOfBirth = dateOfBirth.ToUniversalTime();
                }
                Console.WriteLine("Date of birth is " + dateOfBirth.ToString("o"));

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

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