Introduction to object serialization in JavaScript _ javascript skills

Source: Internet
Author: User
Tags tojson
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.