Overview
The Json.parse () method can parse a JSON string into a JavaScript value. During the parsing process, you can also selectively modify the original parsed values of some properties.
Grammar
Json.parse (text[, reviver])
Parameters
Text
To parse the JSON string, you can look at the JSON article to understand the JSON syntax.
Reviver options available
A function that transforms the parsed property value.
return value
An Object parsed from the text string.
Abnormal
If the parsed JSON string contains a syntax error, a SyntaxError exception is thrown.
Example
Using Json.parse ()
Json.parse (' {} '); // {}
Json.parse (' true '); True
Json.parse (' "foo"); "Foo"
Json.parse (' [1, 5, ' false '] '); [1, 5, "false"]
Json.parse (' null '); Null
Using the Reviver function
If the Reviver function is specified, the parsed JavaScript value (the parsed value) is converted to the final return (return value). More specifically, the parse value itself and all the attributes it contains, will call the Reviver function in a certain order (starting from the most innermost attribute, first level outward, finally reaching the top layer, i.e. parsing the value itself), and the object to which the current property belongs will be used as this value in the call process. The current property name and property values are passed in Reviver as the first and second arguments, respectively. If Reviver returns undefined, the current property is removed from the owning object, and if other values are returned, the returned value becomes the new property value of the current property.
When traversing to the topmost value (parsing value), the parameter passed into the Reviver function is an empty string "" (because there is no real property at this time) and the current parsed value (which may have been modified), the current this value will be {"": Modified parsed value}, when writing the Reviver function, Be aware of this special case.
Json.parse (' {' P ': 5} ', function (k, v) {
if (k = = = ") return V; If the top level is reached, the property value is returned directly,
Return v * 2; Otherwise, the property value becomes twice times the original.
}); {P:10}
Json.parse (' {"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}} ', function (k, v) {
Console.log (k); Output the current property name to know that the traversal order is from inside out,
The last property name will be an empty string.
return v; Returns the original property value, which is equivalent to not passing the Reviver parameter.
});
1
2
4
6
5
3
// ""
Json.parse () does not allow you to end with a comma
Both would throw a syntaxerror
Json.parse ("[1, 2, 3, 4,]");
Json.parse (' {' "foo": 1,} ');
Specification
|
Canonical name |
Canonical state |
Note |
|
ECMAScript 5.1 (ECMA-262)
Json.parse |
Standard |
The first definition, implemented in JavaScript version 1.7. |
|
ECMAScript (6th Edition, ECMA-262)
Json.parse |
Standard |
|
Browser compatibility
|
Feature |
Chrome |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
|
Basic Support |
(Yes) |
3.5 (1.9.1) |
8.0 |
10.5 |
4.0 |
Gecko notes
Starting with Gecko (Firefox 29/thunderbird 29/seamonkey 2.26), if the parsed JSON string contains a syntax error, the error message thrown by the method will contain the exact line number when the error occurred, for debugging a large J The SON data is very useful.
Json.parse (' [1, 2, 3,] ')
SyntaxError:JSON.parse:unexpected character at
Line 1column of the JSON data
RELATED LINKS
Json.parse ()