Just to expand on casperOne's answer .(只是为了扩展casperOne的答案 。)
The JSON spec does not account for Date values.(JSON规范不考虑Date值。)
MS had to make a call, and the path they chose was to exploit a little trick in the javascript representation of strings: the string literal "/" is the same as "\/", and a string literal will never get serialized to "\/" (even "\/" must be mapped to "\\/").(MS必须打个电话,他们选择的路径是利用字符串的javascript表示形式的一个小技巧:字符串文字“ /”与“ \ /”相同,并且字符串文字永远不会序列化为“ \ /”(即使“ \ /”也必须映射到“ \\ /”)。)
See http://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_topic2 for a better explanation (scroll down to "From JavaScript Literals to JSON")(有关更好的说明,请参见http://msdn.microsoft.com/zh-cn/library/bb299886.aspx#intro_to_json_topic2 (向下滚动至“从JavaScript文字到JSON”))
One of the sore points of JSON is the lack of a date/time literal.(JSON的痛处之一是缺少日期/时间文字。)
Many people are surprised and disappointed to learn this when they first encounter JSON.(许多人在初次遇到JSON时会感到惊讶和失望。) The simple explanation (consoling or not) for the absence of a date/time literal is that JavaScript never had one either: The support for date and time values in JavaScript is entirely provided through the Date object.(对于缺少日期/时间文字的简单解释(是否可以安慰)是JavaScript从未使用过:在JavaScript中对日期和时间值的支持完全通过Date对象提供。) Most applications using JSON as a data format, therefore, generally tend to use either a string or a number to express date and time values.(因此,大多数使用JSON作为数据格式的应用程序通常倾向于使用字符串或数字来表示日期和时间值。) If a string is used, you can generally expect it to be in the ISO 8601 format.(如果使用字符串,通常可以期望它采用ISO 8601格式。) If a number is used, instead, then the value is usually taken to mean the number of milliseconds in Universal Coordinated Time (UTC) since epoch, where epoch is defined as midnight January 1, 1970 (UTC).(如果改用数字,则通常取该值表示自纪元以来的世界标准时间(UTC)的毫秒数,其中纪元定义为1970年1月1日午夜(UTC)。) Again, this is a mere convention and not part of the JSON standard.(同样,这只是一个约定,不是JSON标准的一部分。) If you are exchanging data with another application, you will need to check its documentation to see how it encodes date and time values within a JSON literal.(如果要与另一个应用程序交换数据,则需要检查其文档以查看其如何在JSON文字中编码日期和时间值。) For example, Microsoft's ASP.NET AJAX uses neither of the described conventions.(例如,Microsoft的ASP.NET AJAX均不使用所描述的约定。) Rather, it encodes .NET DateTime values as a JSON string, where the content of the string is /Date(ticks)/ and where ticks represents milliseconds since epoch (UTC).(而是将.NET DateTime值编码为JSON字符串,其中字符串的内容为/ Date(ticks)/,而ticks表示自历元(UTC)起的毫秒数。) So November 29, 1989, 4:55:30 AM, in UTC is encoded as "\/Date(628318530718)\/".(因此,1989年11月29日上午4:55:30,以UTC编码为“ \ / Date(628318530718)\ /”。)
A solution would be to just parse it out:(一个解决方案是将其解析出来:)
value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10));
However I've heard that there is a setting somewhere to get the serializer to output DateTime
objects with the new Date(xxx)
syntax.(但是,我听说在某处有一个设置,可让序列化程序以new Date(xxx)
语法输出DateTime
对象。)
I'll try to dig that out.(我会尝试将其挖掘出来。)
The second parameter of JSON.parse()
accepts a reviver
function where prescribes how the value originally produced by, before being returned.(JSON.parse()
的第二个参数接受一个reviver
函数,该函数规定了返回值之前原始值的产生方式。)
Here is an example for date:(这是日期的示例:)
var parsed = JSON.parse(data, function(key, value) {
if (typeof value === 'string') {
var d = //Date((d*))//.exec(value);
return (d) ? new Date(+d[1]) : value;
}
return value;
});
See the docs of JSON.parse()(请参阅JSON.parse()的文档)