JSON.parse() comprehensive usage introduction

JSON is commonly used to exchange data with servers. It is generally a string when receiving server data. We can convert the data into JavaScript objects using the JSON.parse() method.

grammar

JSON.parse(text[, reviver])

text : required, a valid JSON string.
reviver: optional, a function that transforms the result, this function will be called for each member of the object. If it is a function, it specifies how the original value is parsed and transformed before being returned.
If the JSON data received from the server is an array, JSON.parse will convert it into a JavaScript array:

For example:

myArr = JSON.parse(this.responseText);
document.getElementById(“demo”).innerHTML = myArr[1];

If a JSON object is received from the server, JSON.parse will convert it to a JavaScript object:

myObj = JSON.parse(this.responseText);
document.getElementById(“demo”).innerHTML = myObj.name;
使用 JSON.parse()
JSON.parse(‘{}’); // {}
JSON.parse(‘true’); // true
JSON.parse(‘“foo”’); // “foo”
JSON.parse(‘[1, 5, “false”]’); // [1, 5, “false”]
JSON.parse(‘null’); // null
JSON.parse(‘1’); // 1

Use the reviver function

If the reviver function is specified, the parsed JavaScript value (parsed value) will be finally returned (return value) after a transformation. To be more specific: the parsed value itself and all the attributes it contains will be sorted in a certain order (starting from the innermost attribute, going outward one by one, and finally reaching the top layer, which is the parsed value itself). Call the reviver function. During the calling process, the object to which the current attribute belongs will be used as this value, and the current attribute name and attribute value will be passed into reviver as the first and second parameters respectively. If reviver returns undefined, the current property will be deleted from the object to which it belongs. If other values ​​are returned, the returned value will become the new property value of the current property.

When traversing to the topmost value (parsed value), the parameters passed to the reviver function will be an empty string "" (because there is no real attribute at this time) and the current parsed value (which may have been modified) , the current value of this will be {"": the modified parsing value}, and this special case should be noticed when writing the reviver function. (The traversal order of this function is as follows: starting from the innermost layer, according to the hierarchical order, and traversing outward in turn)

JSON.parse('{“p”: 5}', function (k, v) { if(k === '') return v; // If it reaches the top level, return the attribute value directly, return v * 2 ; // Otherwise double the property value. }); // { p: 10 }


JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', function (k, v) { console.log(k); // Output the current attribute name, so that the traversal order is from inside to outside, // The last attribute name will be an empty string. return v; // Return the original attribute value, which is quite Because no reviver parameter was passed. }); // 1 // 2 // 4 // 6 // 5 // 3 // ""










Precautions:

JSON.parse() does not allow a comma at the end, otherwise a syntax error will be reported.
insert image description here
code:

JSON.parse(“[1, 2, 3, 4, ]”);
JSON.parse(‘{“foo” : 1, }’);

Guess you like

Origin blog.csdn.net/cuclife/article/details/131306559