Date.prototype.toJSON()
Date
实例的 toJSON()
方法返回一个表示该日期的字符串,其 ISO 格式与 toISOString()
相同。
¥The toJSON()
method of Date
instances returns a string representing this date in the same ISO format as toISOString()
.
Try it
语法
参数
返回值
¥Return value
一个字符串,表示根据通用时间的 日期时间字符串格式 中的给定日期,或者当日期为 invalid 时的 null
中的给定日期。对于有效日期,返回值与 toISOString()
相同。
¥A string representing the given date in the date time string format according to universal time, or null
when the date is invalid. For valid dates, the return value is the same as that of toISOString()
.
描述
¥Description
当 Date
对象被字符串化时,JSON.stringify()
自动调用 toJSON()
方法。默认情况下,此方法通常旨在在 JSON 序列化期间有效地序列化 Date
对象,然后可以使用 Date()
构造函数作为 JSON.parse()
的恢复器来反序列化该对象。
¥The toJSON()
method is automatically called by JSON.stringify()
when a Date
object is stringified. This method is generally intended to, by default, usefully serialize Date
objects during JSON serialization, which can then be deserialized using the Date()
constructor as the reviver of JSON.parse()
.
该方法首先尝试通过按顺序调用其 [Symbol.toPrimitive]()
(以 "number"
作为提示)、valueOf()
和 toString()
方法来将其 this
值转换为 到一个原始的。如果结果是 non-finite 数字,则返回 null
。(这一般对应无效日期,其 valueOf()
返回 NaN
。)否则,如果转换后的基元不是数字或者是有限数字,则返回 this.toISOString()
的返回值。
¥The method first attempts to convert its this
value to a primitive by calling its [Symbol.toPrimitive]()
(with "number"
as hint), valueOf()
, and toString()
methods, in that order. If the result is a non-finite number, null
is returned. (This generally corresponds to an invalid date, whose valueOf()
returns NaN
.) Otherwise, if the converted primitive is not a number or is a finite number, the return value of this.toISOString()
is returned.
请注意,该方法不会检查 this
值是否是有效的 Date
对象。但是,在非 Date
对象上调用 Date.prototype.toJSON()
会失败,除非该对象的数字基元表示为 NaN
,或者该对象还具有 toISOString()
方法。
¥Note that the method does not check whether the this
value is a valid Date
object. However, calling Date.prototype.toJSON()
on non-Date
objects fails unless the object's number primitive representation is NaN
, or the object also has a toISOString()
method.
示例
使用 toJSON()
序列化往返
¥Serialization round-tripping
解析包含日期字符串的 JSON 时,可以使用 Date()
构造函数将其恢复为原始日期对象。
¥When parsing JSON containing date strings, you can use the Date()
constructor to revive them into the original date objects.
const fileData = {
author: "Maria",
title: "Date.prototype.toJSON()",
createdAt: new Date(2019, 3, 15),
updatedAt: new Date(2020, 6, 26),
};
const response = JSON.stringify(fileData);
// Imagine transmission through network
const data = JSON.parse(response, (key, value) => {
if (key === "createdAt" || key === "updatedAt") {
return new Date(value);
}
return value;
});
console.log(data);
注意:
JSON.parse()
的恢复程序必须特定于你期望的有效负载形状,因为序列化是有损的:无法区分表示日期的字符串和普通字符串。¥Note: The reviver of
JSON.parse()
must be specific to the payload shape you expect, because the serialization is lossy: it's not possible to distinguish between a string that represents a Date and a normal string.
规范
Specification |
---|
ECMAScript Language Specification # sec-date.prototype.tojson |
浏览器兼容性
BCD tables only load in the browser