【问题标题】:How do I convert a JSON DateTime format to a C# DateTime format from an API call如何从 API 调用将 JSON DateTime 格式转换为 C# DateTime 格式
【发布时间】:2021-01-22 07:35:38
【问题描述】:

我目前正在构建一个检索 API 数据并将其保存到数据库中的项目。除了 API 中的 DateTime 值外,一切正常。我有一个使用 RestSharp 获取 API 数据的类,然后它使用 NewtonSoft.Json 将 API 数据反序列化为 JSON 格式,然后将其存储到临时 DTO 类文件中。这是API方法。

public static void getAllRequestData()
{
    var client = new RestClient("[My API URL]");
    var request = new RestRequest();
    var response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        AllRequests.Rootobject result = JsonConvert.DeserializeObject<AllRequests.Rootobject>(rawResponse);
    }
} 

现在这里是 DTO 文件 (AllRequests),它将临时存储转换后的 JSON 数据。

public class AllRequests
    {
        public class Rootobject
        {
            public Operation Operation { get; set; }
        }

        public class Operation
        {
            public Result Result { get; set; }
            public Detail[] Details { get; set; }
        }

        public class Result
        {
            public string Message { get; set; }
            public string Status { get; set; }
        }

        public class Detail
        {
            [Key]
            public int Id { get; set; }
            public string Requester { get; set; }
            public string WorkOrderId { get; set; }
            public string AccountName { get; set; }
            public string CreatedBy { get; set; }
            public string Subject { get; set; }
            public string Technician { get; set; }
            public string IsOverDue { get; set; }
            public string DueByTime { get; set; }
            public string Priority { get; set; }
            public string CreatedTime { get; set; }
            public string IgnoreRequest { get; set; }
            public string Status { get; set; }
        }
    }

Details 中我希望成为 DateTime 格式的代码行是“DueByTime”和“CreatedTime”,而不是字符串值。目前他们只在字符串中保存 JSON 格式的 DateTime 值,例如“1477394860065”。

我尝试将“public string CreatedTime { get; set; }”设置为“public DateTime CreatedTime { get; set; }”但是这只返回了一个错误,因为它是 JSON 格式。我怎样才能纠正这个问题,以便它以 DateTime 格式正确存储在 DTO 中?因为理想情况下我想把这个类文件搭建成一个表,这样它就可以在数据库中保存数据。

有关更多上下文,这是我希望在我的数据库中更正的内容。

我希望显示一个 DateTime,而不是像 Createby 和 DueBy 下的一长串数字。

任何帮助将不胜感激。

【问题讨论】:

标签: c# datetime-format restsharp json-deserialization


【解决方案1】:

[EDIT] 添加了 unix 时间格式合规性[/EDIT]

只需按照@Fildor 所说的输入代码

public long CreatedTime { get; set; }

[JsonIgnore] // will ignore the property below when serializing/deserializing
public DateTimeOffset CreatedTimeDate { 
    // Don't need a setter as the value is directly get from CreatedTime property
    get {
        return DateTimeOffset.FromUnixTimeMilliseconds(CreatedTime);
    }
}

还使用this answer 按照要求使用本地时间转换为 DateTime。

如果不需要偏移量,以下是转换为 DateTime 的方法:https://docs.microsoft.com/fr-fr/dotnet/standard/datetime/converting-between-datetime-and-offset

【讨论】:

  • 感谢您的回答,但是,我在添加时遇到错误,它说:“无法从 'string' 转换为 'double'
  • 错误我疯了。我现在纠正它。我保留了类型字符串而不是 long 或 double。所以 DateTime 转换失败,因为它预期是双倍
  • 我刚刚试了一下,没有错误,但由于某种原因仍然没有转换。当我搭建数据库并拉取API数据时,它仍然设置为数字格式而不是DateTime,很奇怪
  • 您的值不是秒而是毫秒。我还建议使用FromUnixTimeMilliseconds
  • 我已经更新了我的问题,您可以看到我正在谈论的表格的图像。我希望它采用 DateTime 格式,而不是现在的一长串数字。由于某种原因,该返回值似乎没有返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 2016-09-08
  • 2012-10-17
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2021-06-02
相关资源
最近更新 更多