This article mainly introduces Object serialization in JavaScript. Object serialization in JavaScript is through JSON. stringify (), and deserialization is implemented through JSON. parse (). If you need it, you can refer to the same Java language. In JavaScript, You Can serialize and deserialize the object to save the object. In ECMAScript 5, Object serialization in JavaScript is implemented through JSON. stringify (), and deserialization is implemented through JSON. parse:
The Code 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 Douglas Crockford to write json2.js (https://github.com/douglascrockford/JSON-js ).
During Object serialization, NaN, Infinity, and-Infinity are serialized to "null"; Date objects are serialized to strings representing the corresponding time (however, JSON is used. during parse () deserialization, the time string will exist as a normal string and will not be rebuilt as a Date object ).
When JSON. stringify () is used to serialize an object, the serialized property is limited to the property of the enumerable of the object itself (Own. In JSON. when stringify () is running, JavaScript first checks whether the toJSON () method exists in the object to be serialized. If the toJSON () method exists, the method is called and the returned result is used as the serialization target. If the toJSON () method does not exist, use the default serialization method.