Home>Article>Web Front-end> What are the methods of json in javascript

What are the methods of json in javascript

青灯夜游
青灯夜游 Original
2021-04-09 18:44:03 3766browse

There are two json methods built into JavaScript: 1. "JSON.parse()" method, used to convert a JSON string into a JavaScript object; 2. "JSON.stringify()" method, used For converting JavaScript values into JSON strings.

What are the methods of json in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JS has two built-in Json methods

1. JSON.parse()--Convert string to object

JSON.parse() is used to convert a JSON string into a JavaScript object.

If we receive a string in JSON format, we only need to deserialize it into a JavaScript object, and then we can use this object directly in JavaScript

JSON.parse(text[, reviver])

Parameter description:

  • text:Required, a valid JSON string.

  • reviver: Optional, a function that converts the result. This function will be called for each member of the object.

Return value: Returns the converted object of the given JSON string.

Example:

What are the methods of json in javascript

2. JSON.stringify()--Convert the object into a string

JSON .stringify() is used to convert JavaScript values to JSON strings.

Any way to turn JavaScript into Json is to serialize this object into a Json string before it can be transmitted through the network;

JSON.stringify(value[, replacer[, space]])

Parameter description:

What are the methods of json in javascript

Return value: Returns a string containing JSON text.

Example:

What are the methods of json in javascript

[Recommended learning:javascript advanced tutorial

The above is the detailed content of What are the methods of json in javascript. For more information, please follow other related articles on the PHP Chinese website!

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