Home >Web Front-end >JS Tutorial >How to use json.stringify in JavaScript

How to use json.stringify in JavaScript

不言
不言Original
2019-01-07 14:00:104244browse

Encoding is a method of converting data into another form. In the case of JSON, JSON is described as a JavaScript object, but the data format of its content is just a string. Actually, reading a json file is just a string, so it can't be treated like an object. Encoding will allow you to use it as an object in JavaScript. Let's take a closer look at how to use JavaScript to encode json.

How to use json.stringify in JavaScript

To encode we use JSON.parse.

var json_s = '{"ID": 1,"name": "user01","gender": "women"}'
console.log(typeof(json_s))
 
var json_o = JSON.parse(json_s)
console.log(typeof(json_o))
console.log(json_o.ID)

json_s defines json, which is just a string encoded into an object via JSON.parse.

How to use json.stringify in JavaScript

After reading the encoding, let’s look at how to decode

Decoding is to return the encoded version to its original form.

We use the JSON.stringify function to return a string.

Let’s take a look at the code

var json = '{"ID": 1,"name": "user01","gender": "women"}'
console.log(typeof(json))
 
var encoded_json = JSON.parse(json)
console.log(typeof(encoded_json))
 
var decoded_json = JSON.stringify(encoded_json)
console.log(typeof(decoded_json))

The running results are as follows

How to use json.stringify in JavaScript

You can see that the decoding is completed correctly.

After reading the above content, let’s get to the point. What is json.stringify? how to use?

json stringify is a function that decodes JSON.

Let’s format it while decoding it using json stringify

JSON.stringify() can take three parameters.

The first is the encoded value, which is an object, as in the example.

The second one can be called replacer and specify the callback.

I won’t explain it in detail here, so I will add null to the code.

The third one is used to specify the indentation for formatting this parameter.

Let’s look at the specific code

var json_obj = {"ID": 1,"name": "user01","gender": "women"}
 
var decoded_json = JSON.stringify(json_obj, null, "  ")
console.log(decoded_json)

The running result is as follows: it will be indented according to the spaces placed

How to use json.stringify in JavaScript

Put more spaces and you will see it more clearly. The code is as follows

var json_obj = {"ID": 1,"name": "user01","gender": "women"}
 
var decoded_json = JSON.stringify(json_obj, null, "               ")
console.log(decoded_json)

The result is as follows

How to use json.stringify in JavaScript

In addition, although you can add spaces, It is difficult to see how many indents are explicitly specified, so you can also directly specify the value for indentation

var json_obj = {"ID": 1,"name": "user01","gender": "women"}
 
var decoded_json = JSON.stringify(json_obj, null, 2)
console.log(decoded_json)

The results are as follows

How to use json.stringify in JavaScript

Let’s take a lookUse json.stringify to decode an array

The code is as follows

var arr = ["a","b","c"];
 
var decoded_json = JSON.stringify(arr)
console.log(decoded_json)
console.log(typeof(decoded_json))

The running results are as follows: Correct decoding

How to use json.stringify in JavaScript

It doesn’t matter whether the object contains an array

The code is as follows

var arr = {"test": ["a","b","c"]};
 
var decoded_json = JSON.stringify(arr)
console.log(decoded_json)
console.log(typeof(decoded_json))

The running result is as follows

How to use json.stringify in JavaScript

The above is the detailed content of How to use json.stringify 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