Home > Article > Web Front-end > Detailed explanation of conversion between JSON strings and objects_json
JSON (JavaScript Object Notation) is a subset of the JavaScript programming language. Because JSON is a subset of JavaScript, it can be clearly used in this language.
eval function Convert JSON text to object
In order to convert JSON text into an object, you can use the eval function. The eval function calls the JavaScript editor. Since JSON is a subset of JavaScript, the compiler will correctly parse the text and produce object structures. Text must be enclosed in parentheses to avoid JavaScript syntax ambiguities.
var obj = eval('(' JSONTest ')'); The eval function is very fast. It can compile and execute any JavaScript program, thus creating security issues. The eval function should only be used when using trusted and complete source code. This allows for safer parsing of JSON text. For web applications that use XmlHttp, communication between pages only allows the same origin, so it can be trusted. But it is not perfect. If the server does not have strict JSON encoding, or does not have strict input validation, it may transmit invalid JSON text including dangerous scripts. The eval function will execute the malicious script.
JSON interpreter JSON.parse, JSON.stringify
Using a JSON parser can prevent security risks like the eval function that converts JSON text into objects. The JSON parser can only recognize JSON text and reject all scripts. Browsers that provide native JSON support will have their JSON parsers much faster than the eval function.
Currently, Firefox, Opera, and IE8 and above also provide local JSON support. Among them, the functions provided by the JSON interpreter are: JSON.parse, JSON.stringify.
For browsers that do not provide native JSON support, you can introduce the script json2.js to implement the JSON conversion function. The json2.js script can be downloaded from the https://github.com/douglascrockford/JSON-js/blob/master/json2.js page.
JSON.parse function
Convert JSON text to object.
JSON.parse(text[, reviver])
Parameters
text
Required. JSON text to be converted to an object.
reviver
Optional. This parameter is a replacement function. In the transformation, for each node traversed, this function will be executed, and the return value of the function will replace the corresponding node value of the transformation result.
JSON.stringify function
Convert object to JSON text.
JSON.stringify(value[, replacer[, space]])
Parameters
text
Required. The object to be converted to JSON text.
reviver
Optional. This parameter is a replacement function. In the transformation, for each node traversed, this function will be executed, and the return value of the function will replace the corresponding node value of the transformation result.
space
Optional. The number of spaces to indent the formatted output JSON text. If this parameter is not provided the output will not be formatted.
Delegate type of parameter reviver
reviver(key, value)
This in the reviver function is the parent node of the node currently traversed. When the root node is traversed, the parent node is an Object object, the root node is an attribute of the object, and the attribute name is an empty string.
Parameters
key
When the parent node is an array Object, the key is the array index, otherwise the key is the Object property name.
value
node value.
Note: JSON does not support circular data structures.
jQuery.parseJSON( jsonTex )
jQuery also has a method for converting strings to JSON format, jQuery.parseJSON(json), which accepts a standard format JSON string and returns a parsed JavaScript (JSON) object. Of course, if you are interested, you can encapsulate a jQuery extension yourself. jQuery.stringifyJSON(obj) converts JSON into a string.
The above is the entire content of this article. I hope you all like it.