如何将 unix 时间戳 (μs) 从 JSON 反序列化为 DateTime?

How to deserialize a unix timestamp (μs) to a DateTime from JSON?(如何将 unix 时间戳 (μs) 从 JSON 反序列化为 DateTime?)

本文介绍了如何将 unix 时间戳 (μs) 从 JSON 反序列化为 DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
   "title":"Mozilla Firefox",
   "id":24,
   "parent":2,
   "dateAdded":1356753810000000,
   "lastModified":1356753810000000,
   "type":"text/x-moz-place-container",
   "children":[]
}

C#

class Bookmark
{
    public string title;
    public string id;
    [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
    public DateTime dateAdded;
    [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
    public DateTime lastModified;
    public string type;
    public string root;
    public long parent;
    public List<Bookmark> children;
}

private static void Main(string[] args)
{
    var json = File.ReadAllText(@"T:/bookmarks-2013-11-13.json");
    var bookmarks = JsonConvert.DeserializeObject<Bookmark>(json);
}

当我尝试运行此程序时出现异常,

I get an exception when I try running this,

附加信息:错误读取日期.意外标记:整数.路径添加日期"

Additional information: Error reading date. Unexpected token: Integer. Path 'dateAdded'

我认为通过使用 JavaScriptDateTimeConverter,JSON.NET 可以弄清楚如何反序列化那些 unix 时间戳(自纪元以来的 ms μs).最简单的方法是什么?

I thought by using the JavaScriptDateTimeConverter, JSON.NET could figure out how to deserialize those unix timestamps (ms μs since epoch). What's the easiest way to do this?

很难找到有关转换器的文档...如果有必要,自己编写一个可能不会太难.

Having trouble finding documentation on the converters... it probably wouldn't be too hard to write one myself if necessary.

这些实际上是微秒,而不是毫秒.

Those are actually microseconds, not milliseconds.

推荐答案

我清理了一点Cris 的解决方案 并实现了WriteJson:

I cleaned up Cris's solution a tad and implemented WriteJson:

class Bookmark
{
    public string title;
    public long id;
    [JsonConverter(typeof(MicrosecondEpochConverter))]
    public DateTime dateAdded;
    [JsonConverter(typeof(MicrosecondEpochConverter))]
    public DateTime lastModified;
    public string type;
    public string root;
    public long parent;
    public List<Bookmark> children;
    public string uri;

    public override string ToString()
    {
        return string.Format("{0} - {1}", title, uri);
    }
}

public class MicrosecondEpochConverter : DateTimeConverterBase
{
    private static readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteRawValue(((DateTime)value - _epoch).TotalMilliseconds + "000");
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.Value == null) { return null; }
        return _epoch.AddMilliseconds((long)reader.Value / 1000d);
    }
}

internal class Program
{

    private static void Main(string[] args)
    {
        var jsonString = File.ReadAllText(@"T:/bookmarks-2013-11-13.json");
        var rootMark = JsonConvert.DeserializeObject<Bookmark>(jsonString);
        var ret = JsonConvert.SerializeObject(rootMark);
    }
}

这篇关于如何将 unix 时间戳 (μs) 从 JSON 反序列化为 DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将 unix 时间戳 (μs) 从 JSON 反序列化为 DateTime?