首页 新闻 会员 周边

关于Json中Date对象的格式

0
悬赏园豆:20 [待解决问题]

请教各位博友2个问题:

1.Json中表示日期的格式Date(1583570856087+0800) 这个是啥标准?

2.Java用啥工具可以将Entity中的Date类型序列化为Date(1583570856087+0800) 这种json的日期格式?

补充:
具体了解下来这个格式好像是微软的处理方式(https://docs.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer?redirectedfrom=MSDN&view=netframework-4.8
Date object, represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).

WCF里面关于日期格式的序列化方式:(https://www.codenong.com/44934054/
public static string MsJson(DateTime value)
{
long unixEpochTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
long ticks = (value.ToUniversalTime().Ticks - unixEpochTicks) / 10000;

if (value.Kind == DateTimeKind.Utc)
{
    return String.Format("/Date({0})/", ticks);
}
else
{
    TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());
    string sign = ts.Ticks < 0 ?"-" :"+";
    int hours = Math.Abs(ts.Hours);
    string hs = (hours < 10) 
        ?"0" + hours 
        : hours.ToString(CultureInfo.InvariantCulture);
    int minutes = Math.Abs(ts.Minutes);
    string ms = (minutes < 10) 
        ?"0" + minutes 
        : minutes.ToString(CultureInfo.InvariantCulture);
    return string.Format("/Date({0}{1}{2}{3})/", ticks, sign, hs, ms);
}

}

参考链接:
https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/bb299886(v=msdn.10)?redirectedfrom=MSDN#intro_to_json_sidebarb

笑清风的主页 笑清风 | 初学一级 | 园豆:184
提问于:2020-03-07 17:44
< >
分享
所有回答(3)
0
风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-07 18:50

好像和问题不相干哦,不过谢谢你

支持(0) 反对(0) 笑清风 | 园豆:184 (初学一级) | 2020-03-07 21:11
0

+800代表中国标准时间,标准是ECMAScript,具体见Date.prototype.toJSON()规范

ycyzharry | 园豆:25653 (高人七级) | 2020-03-08 14:44
0

The AJAX JSON serializer in ASP.NET encodes a DateTime instance as a JSON string. During its pre-release cycles, ASP.NET AJAX used the format "@ticks@", where ticks represents the number of milliseconds since January 1, 1970 in Universal Coordinated Time (UTC). A date and time in UTC like November 29, 1989, 4:55:30 AM would be written out as "@62831853071@." Although simple and straightforward, this format cannot differentiate between a serialized date and time value and a string that looks like a serialized date but is not meant to be deserialized as one. Consequently, the ASP.NET AJAX team made a change for the final release to address this problem by adopting the "/Date(ticks)/" format.

The new format relies on a small trick to reduce the chance for misinterpretation. In JSON, a forward-slash (/) character in a string can be escaped with a backslash () even though it is not strictly required. Taking advantage of this, the ASP.NET AJAX team modified JavaScriptSerializer to write a DateTime instance as the string "/Date(ticks)/" instead. The escaping of the two forward-slashes is superficial, but significant to JavaScriptSerializer. By JSON rules, "/Date(ticks)/" is technically equivalent to "/Date(ticks)/" but the JavaScriptSerializer will deserialize the former as a DateTime and the latter as a String. The chances for ambiguity are therefore considerably less when compared to the simpler "@ticks@" format from the pre-releases.

笑清风 | 园豆:184 (初学一级) | 2020-03-08 21:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册