A detailed explanation of the conversion between JSON strings and objects

Source: Internet
Author: User
Tags data structures eval json string to json

This article mainly introduces the JSON string and the conversion between objects in detail, the need for friends can refer to the following

JSON (JavaScript Object notation) is a subset of the JavaScript programming language. Because JSON is a subset of JavaScript, it can be used clearly in this language.

eval function JSON text converted to object

To convert JSON text to objects, you can use the Eval function. The Eval function calls the JavaScript editor. Because JSON is a subset of JavaScript, the compiler will parse the text correctly and produce an object structure. Text must be enclosed in parentheses to avoid grammatical ambiguity in JavaScript.

var obj = eval (' + jsontest + '); The Eval function is very fast. It can compile and execute any JavaScript program, thus creating a security issue. You can use the Eval function when you are using trusted and sophisticated source code. This makes it more secure to parse the JSON text. With XMLHTTP Web applications, communication between pages only allows homology and therefore can be trusted. But this is not perfect. If the server does not have a rigorous JSON encoding, or if there is no strict input validation, then it is possible to route invalid JSON literals that include dangerous scripts. The Eval function will execute a malicious script.

JSON interpreter Json.parse, json.stringify

You can use the JSON parser to prevent security risks like the Eval function from converting JSON text as objects. The JSON parser can only recognize JSON text and reject all scripts. The JSON parser for browsers that provide local JSON support will be much faster than the Eval function.

At present, Firefox, Opera, IE8 version of the above also provides local JSON support. Among the functions provided by the JSON interpreter are: Json.parse, json.stringify.

For browsers that do not provide local JSON support, you can introduce script json2.js to implement the JSON conversion functionality. Json2.js scripts can be downloaded to the Https://github.com/douglascrockford/JSON-js/blob/master/json2.js page.

Json.parse function

Converts the JSON text to an object.

Json.parse (text[, Reviver])

Parameters

Text

Required option. The JSON text to convert to an object.

Reviver

Options available. The parameter is an alternate function. In a transformation, each node that is traversed executes the function, and the return value of the function replaces the corresponding node value of the transformation result.

json.stringify function

Converts an object to JSON text.

Json.stringify (value[, replacer[, space])

Parameters

Text

Required option. The object to convert to JSON text.

Reviver

Options available. The parameter is an alternate function. In a transformation, each node that is traversed executes the function, and the return value of the function replaces the corresponding node value of the transformation result.

MySpace

Options available. Formats output the number of spaces in the JSON text indentation. If this parameter is not supplied, the output will not be formatted.

delegate type for parameter Reviver

Reviver (key, value)

This in the Reviver function is the parent of the node that is currently being traversed. When the root node is traversed, the parent node is an object, and the root node is an attribute of the object, and the property name is an empty string.

Parameters

Key

The key is the object property name when the parent node is an array object,key.

Value

The node value.

Note: JSON does not support circular data structures.

Jquery.parsejson (Jsontex)

jquery also has a method Jquery.parsejson (JSON) that converts a string to JSON format, accepts a well-formed JSON string, and returns the parsed JavaScript (JSON) object. Of course, if you are interested in encapsulating a jQuery extension yourself, Jquery.stringifyjson (obj) converts JSON to a string.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.