<legend id='Gr4AX'><style id='Gr4AX'><dir id='Gr4AX'><q id='Gr4AX'></q></dir></style></legend>

    <tfoot id='Gr4AX'></tfoot>
    <i id='Gr4AX'><tr id='Gr4AX'><dt id='Gr4AX'><q id='Gr4AX'><span id='Gr4AX'><b id='Gr4AX'><form id='Gr4AX'><ins id='Gr4AX'></ins><ul id='Gr4AX'></ul><sub id='Gr4AX'></sub></form><legend id='Gr4AX'></legend><bdo id='Gr4AX'><pre id='Gr4AX'><center id='Gr4AX'></center></pre></bdo></b><th id='Gr4AX'></th></span></q></dt></tr></i><div id='Gr4AX'><tfoot id='Gr4AX'></tfoot><dl id='Gr4AX'><fieldset id='Gr4AX'></fieldset></dl></div>

    <small id='Gr4AX'></small><noframes id='Gr4AX'>

      • <bdo id='Gr4AX'></bdo><ul id='Gr4AX'></ul>

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

      时间:2023-08-24

          <tfoot id='KndUK'></tfoot>
          <i id='KndUK'><tr id='KndUK'><dt id='KndUK'><q id='KndUK'><span id='KndUK'><b id='KndUK'><form id='KndUK'><ins id='KndUK'></ins><ul id='KndUK'></ul><sub id='KndUK'></sub></form><legend id='KndUK'></legend><bdo id='KndUK'><pre id='KndUK'><center id='KndUK'></center></pre></bdo></b><th id='KndUK'></th></span></q></dt></tr></i><div id='KndUK'><tfoot id='KndUK'></tfoot><dl id='KndUK'><fieldset id='KndUK'></fieldset></dl></div>
          • <legend id='KndUK'><style id='KndUK'><dir id='KndUK'><q id='KndUK'></q></dir></style></legend>
              <tbody id='KndUK'></tbody>
              • <bdo id='KndUK'></bdo><ul id='KndUK'></ul>

                <small id='KndUK'></small><noframes id='KndUK'>

                本文介绍了如何将 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Newtonsoft.Json 中处理十进制值 下一篇:如何将Json数组转换为C#中的对象列表

                相关文章

                最新文章

                <tfoot id='8PsfK'></tfoot>

                  <small id='8PsfK'></small><noframes id='8PsfK'>

                1. <i id='8PsfK'><tr id='8PsfK'><dt id='8PsfK'><q id='8PsfK'><span id='8PsfK'><b id='8PsfK'><form id='8PsfK'><ins id='8PsfK'></ins><ul id='8PsfK'></ul><sub id='8PsfK'></sub></form><legend id='8PsfK'></legend><bdo id='8PsfK'><pre id='8PsfK'><center id='8PsfK'></center></pre></bdo></b><th id='8PsfK'></th></span></q></dt></tr></i><div id='8PsfK'><tfoot id='8PsfK'></tfoot><dl id='8PsfK'><fieldset id='8PsfK'></fieldset></dl></div>
                  • <bdo id='8PsfK'></bdo><ul id='8PsfK'></ul>
                2. <legend id='8PsfK'><style id='8PsfK'><dir id='8PsfK'><q id='8PsfK'></q></dir></style></legend>