Conversion between JSON strings and objects.
JSON (JavaScript Object Notation) is a subset of JavaScript programming languages. JSON is a subset of JavaScript, so it can be clearly used in this language.
Eval function JSON text conversion to object
To convert JSON text to 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 parse the text correctly and generate the object structure. The text must be enclosed in brackets to avoid syntax ambiguity in JavaScript.
Var obj = eval ('+ JSONTest +'); the eval function is very fast. It can compile and execute any JavaScript program, resulting in security issues. The eval function can be used only when trusted and complete source code is used. In this way, JSON text can be parsed more securely. For web applications that use XmlHttp, communication between pages can only be the same source, so it is trustworthy. However, this is not perfect. If the server does not have strict JSON encoding or strict input verification, invalid JSON text including dangerous Scripts may be transmitted. The eval function executes malicious scripts.
JSON interpreter JSON. parse, JSON. stringify
JSON parser can be used to prevent security risks such as eval functions converting JSON text into objects. The JSON parser can only recognize JSON text and reject all scripts. The browser's JSON parser that provides local JSON support will be much faster than the eval function.
Currently, Firefox, Opera, and IE8 and later versions also provide local JSON support. The JSON interpreter provides the following functions: JSON. parse and JSON. stringify.
For browsers that do not provide local JSON support, you can introduce the json2.js script to implement the JSON conversion function. Json2.js scripts can be found.
JSON. parse function
Converts JSON text to an object.
JSON. parse (text [, reviver])
Parameters
Text
Required. JSON text to be converted to an object.
Reviver
Optional. This parameter is a replacement function. During conversion, this function is executed for each node that is traversed. the return value of this function replaces the value of the corresponding node in the conversion result.
JSON. stringify Function
Converts an object to JSON text.
JSON. stringify (value [, replacer [, space])
Parameters
Text
Required. The object to convert to JSON text.
Reviver
Optional. This parameter is a replacement function. During conversion, this function is executed for each node that is traversed. the return value of this function replaces the value of the corresponding node in the conversion result.
Space
Optional. Number of spaces for formatting and outputting the indent of JSON text. If this parameter is not provided, the output is not 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, and the root node is an attribute of the Object. The attribute name is a null string.
Parameters
Key
When the parent node is an array Object, the key is an array index; otherwise, the key is the Object attribute name.
Value
Node value.
Note: JSON does not support cyclic data structures.
JQuery. parseJSON (jsonTex)
JQuery also provides the JSON format conversion method jQuery. parseJSON (json). It accepts a standard JSON string and returns the parsed JavaScript (JSON) object. If you are interested, you can encapsulate a jQuery extension. jQuery. stringifyJSON (obj) converts JSON into a string.
The above is all the content of this article. I hope you will like it.