Home >Web Front-end >JS Tutorial >js parsing and serializing json data (3) Discussion on json parsing_json

js parsing and serializing json data (3) Discussion on json parsing_json

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

In this section we mainly discuss the parsing of json.
The JSON.parse() method can also receive another parameter, which is a function that will be called on each key-value pair. In order to distinguish the replacement (filter) function (replacer) received by JSON.stringify(), this function is called the restore function (reviver), but in fact the signatures of the two functions are the same - they both receive two parameters, A key and a value, and both need to return a value.

If the restore function returns undefined, it means that the corresponding key is to be removed from the result; if another value is returned, the value is inserted into the result. For example:

Copy code The code is as follows:










The above code first adds the birthdate birthDate attribute to the student. This property holds a Date object. This object becomes a valid JSON string after serialization, and then is parsed and restored to a Date object in studentObject.
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