Home  >  Article  >  Web Front-end  >  js parsing and serializing json data (1) Basic usage of json.stringify()_json

js parsing and serializing json data (1) Basic usage of json.stringify()_json

WBOY
WBOYOriginal
2016-05-16 17:42:202393browse

Early JSON parsers basically used JavaScript's eval() function. Since JSON is JavaScript syntax itself, the eval() function can parse, interpret, and return JavaScript objects and arrays.
ECMAScript 5 standardizes the behavior of parsing JSON and defines the global object JSON.

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 a new HTML: the code is as follows:

Copy the code The code is as follows:




< /head>

Default case, the JSON string output by JSON.stringify() does not contain any null characters or indentation, so the string saved in jsonBook looks like this:
Copy Code The code is as follows:

{"title":"Advanced Programming with JavaScript","authors":["Nicholas C. Zakas"],"edition" :3,"year":2011}

When serializing JavaScript objects, all functions and prototype members will be intentionally ignored and will not be reflected in the result. Additionally, any properties with a value of undefined will also be skipped. The results end up with instance properties whose values ​​are valid JSON data types.

Note that although book and objectBook have the same properties, they are two independent objects with no relationship. If the string passed to JSON.parse() is not valid JSON, this method will throw an error.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn