Parse and serialize JSON data in JS (1) basic usage of JSON. stringify ()

Source: Internet
Author: User

The early JSON parser basically used JavaScript's eval () function. Because JSON is a javascript syntax, the eval () function can parse, interpret, and return JavaScript objects and arrays.
Ecmascript 5 standardizes the JSON parsing behavior and defines the Global Object JSON.

the JSON object has two methods: stringify () and parse (). In the simplest case, these two methods are used to serialize JavaScript objects into JSON strings and parse JSON strings into Native JavaScript. For example,
Create an HTML file: the Code is as follows: copy Code the code is as follows:


By default, the JSON string output by JSON. stringify () does not contain any blank characters or indentation. Therefore, the strings stored in jsonbook are as follows:Copy codeThe Code is as follows: {"title": "javascript advanced programming", "Authors": ["Nicholas C. zakas"], "edition": 3, "year": 2011}

When serializing JavaScript objects, all functions and prototype members are intentionally ignored and not reflected in the results. In addition, any attribute with the value of undefined will be skipped. In the result, all instance properties with valid JSON data types are final.

Note that although the book and objectbook have the same attributes, they are two independent objects with no relationship. If the string passed to JSON. parse () is not a valid JSON string, this method throws an error.

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.