JsonConverter序列化日期字段以返回刻度数

时间:2017-05-01 09:12:43

标签: c# json serialization

有没有办法迫使NewtonSoft的JsonConverter"\/Date(number of ticks)\/"格式的日期时间标记为JavascriptSerializer呢?

string resp1 = JsonConvert.SerializeObject(new
    {
        data = collection.Select(r => new
        {
            Date = r.Date,
        })
    }, 
    Formatting.Indented, 
    new JsonSerializerSettings 
    { 
        PreserveReferencesHandling = PreserveReferencesHandling.Objects 
    });

UPDTE1:

我可以做一个解决方法,但我认为这不是一种有效的方法。

 string resp = JsonConvert.SerializeObject(new
                    {
                        data = collection.Select(r => new
                        { 
                            Date = new JavaScriptSerializer().Serialize(r.Date).ToString()
                        })
                    }, Formatting.Indented,
                    new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });

1 个答案:

答案 0 :(得分:0)

请参阅guide on the official site,此处他们使用示例:

string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());

这告诉序列化程序将日期时间存储为刻度。 但您应该阅读链接信息,因为在使用存储格式时需要了解一些事项