Like the Java language, objects can be serialized and deserialized in JavaScript, thereby saving objects. In the ECMAScript 5 standard, object serialization in JavaScript is implemented through JSON.stringify(), while deserialization is implemented through JSON.parse():
The code copy is as follows:
var o = {x:1, y:29, z:42};
var s = JSON.stringify(o);
console.log(s);//{"x":1,"y":29,"z":42}
var c = JSON.parse(s);
console.log(c);//Object {x=1, y=29, z=42}
For browsers that only support the ECMAScript 3 standard, you can use json2.js written by Douglas Crockford (https://github.com/douglascrockford/JSON-js).
During the serialization process of the object, NaN, Infinity, and -Infinity will be serialized to "null"; the Date object will be serialized to a string representing the corresponding time (but when deserialized using JSON.parse(), the time string will exist as a normal string and will not be rebuilt into a Date object).
When using JSON.stringify() to serialize an object, the serialized property is limited to the enumerable property of the object itself (Own). When JSON.stringify() is run, JavaScript will first find out whether there is a toJSON() method in the object that needs to be serialized. If the toJSON() method exists, the method will be called and the result it returns is used as the target of serialization. If the toJSON() method does not exist, the default serialization method is used.