Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Examples of the usage of JSON.parse () in JavaScript

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the examples of the usage of JSON.parse () in JavaScript. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Detailed explanation of the use of JSON.parse () in JavaScript

JSON.parse ('{}') / / {}

JSON.parse ('true') / / true

JSON.parse ('null') / / null

JSON.parse syntax

JSON.parse (text [, reviver])

Text: the string to be parsed into.

If a number is passed in, it is converted to a decimal digital output.

If a Boolean value is passed in, it is output directly.

If null is passed in, null is output.

Other types of values are not supported, otherwise an error is reported.

Reviver: optional, converter, which can be used to modify the original values generated by parsing.

Return value: JavaScript object / value, corresponding to the object / value of the given JSON text.

Reviver parameter

The reviver function, which is used to transform the JavaScript value parsed by processing, and returns the final result after processing.

Conversion process:

The parsed value itself and all the attributes it may contain call the reviver function in a certain traversal order, and the attribute name and value are passed as parameters, key and value.

Traversal order: according to the hierarchy, traversing from the inside to the outside, and finally reaching the top level, is the parsed value itself.

If reviver returns undefined, the object is deleted, and if another value is returned, the value becomes the new value of the current property.

When traversing to the top level, because there are no attributes, the parameter key is an empty string''and the parameter value is the current parsed value.

For the two parameters of the reviver function, key and value, different data types:

Basic value type data (string, number, boolean) and null, as well as empty object {} and empty array []:

Then key is an empty string and value is the corresponding parsed value.

Because it is already at the top level, there are no other attributes.

Object object:

Then both key and value exist, corresponding to the property name and value respectively.

The top level returns a value with an empty parameter key.

Array:

Key corresponds to the array index and value corresponds to the element value.

The top level returns a value with an empty parameter key.

Conversion of basic types:

JSON.parse ('5percent, function (key, value) {

Console.log (`key:$ {key}, value:$ {value} `)

})

/ / key:, value:5

JSON.parse ('null', function (key, value) {

Console.log (`key:$ {key}, value:$ {value} `)

})

/ / key:, value:null

JSON.parse ('{}', function (key, value) {

Console.log (`key:$ {key}, value: `, value)

})

/ / key:, value: {}

Object objects and arrays:

JSON.parse ('[1,2]', function (key, value) {

Console.log (`key:$ {key}, value: `, value)

})

/ / key:0, value: 1

/ / key:1, value: 2

/ / key:, value: (2) [empty × 2]

JSON.parse ('{"user": "Zhang San", "info": {"age": 25, "sex": 1}}', function (key, value) {

Console.log (`key:$ {key}, value:: `, value)

})

/ / key:user, value:: Zhang San

/ / key:age, value:: 25

/ / key:sex, value:: 1

/ / key:info, value:: {}

/ / key:, value:: {}

Data processing:

JSON.parse ('[1,2]', function (key, value) {

If (key = =) {

Return value

}

Return value + 3

})

/ / [4, 5]

This is the end of the article on "examples of the use of JSON.parse () in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report