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?
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01