Performance API
[!WARNING] This interface of this property is deprecated in the Navigation Timing Level 2 specification. Please use the PerformanceNavigationTiming interface instead.
The toJSON() method of the PerformanceNavigation interface returns a JSON-serializable plain object representing the PerformanceNavigation object.
The method is automatically called by JSON.stringify() when a PerformanceNavigation object is encountered during JSON serialization.
Syntax
toJSON()
Parameters
None.
Return value
A JSON-serializable plain object containing the following properties:
- PerformanceNavigation/type
- PerformanceNavigation/redirectCount
Each property's value is copied as-is.
Examples
Calling toJSON() directly
Calling toJSON() directly on performance.navigation returns a plain object. Its properties have the same values as the corresponding properties of the original object.
const json = performance.navigation.toJSON();
console.log(json); // A plain object
console.log(typeof json); // "object"
console.log(json.type); // Same value as performance.navigation.type
Serializing to a JSON string
In this example, the PerformanceNavigation object is serialized using JSON.stringify(), which calls the toJSON() method automatically.
console.log(JSON.stringify(performance.navigation));
This would log a JSON string like so (formatted for readability):
{
"type": 0,
"redirectCount": 0
}
Specifications
Browser compatibility
See also
- JSON
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
