How to deserialize a unix timestamp (μs) to a DateTime from JSON?(如何将 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?
基础教程推荐
- C# 从 List<List<int>> 中删除重 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
