JSON structure of javascript

Source: Internet
Author: User
Tags javascript array javascript eval
Use JSON

JSON is a text format used for data exchange. Its role is to provide a representation of structured data, instead of relying on the language and platform used. Using this format, you can exchange data between applications written in different languages and run applications on different machines. Compared with XML (which may be the most famous data exchange format), JSON syntax is concise. This indicates that transmitting JSON data over the network usually requires less bandwidth.

JSON is built on a subset of the Javascript language. Therefore, its encoding and resolution are very direct. Since most Ajax developers are also JavaScript developers, there is almost no learning curve in JSON.

3.2.1 JSON Structure

The base of JSON includes two structures: a set of name-value pairs, called objects, and an ordered list of values, called arrays ). In JSON, the value can be in the following form:

Object

Array

Number

String

True

False

Null

The object is the JavaScript Object literal, the array is the Javascript array literal, and the remaining values are also represented by the corresponding literal.

Because JSON is a subset of JavaScript literal, there are some syntax restrictions. In a JSON object, the name part of the name-value pair must be a string, and the value part must be a supported value. The following is a JSON representation of an object, which contains two attributes:

Attribute names (firstname and lastname) must be strings and must be enclosed in double quotation marks. The following code represents a similar object and compares the previous code with it:

In JavaScript, the two objects have the same structure. However, the second object is not a valid JSON representation because the attribute name is not enclosed in double quotation marks.

JSON arrays also have some restrictions. The elements in the array must be supported values. For example, the date object is not a column of supported values, so it cannot be an element of a JSON array or an attribute of a JSON object. String has the same representation as the javascript string literal, but the string must be enclosed by double quotation marks. Number is similar to the Javascript numeric literal, but the octal and hexadecimal formats are not supported. The following is an example of a JSON array:

Boolean values (true and false) and null have the same representation as the corresponding JavaScript literal.

The description method cannot be represented in JSON because the JSON syntax does not include the function literal. In addition, the new operator of JavaScript is not part of the JSON syntax and cannot be used in objects or arrays.

One of the advantages of JSON is that it is easy to parse. Many JSON parsers have been compiled for many languages, which can automatically generate and parse JSON. (A list of these parsers can be found on the JSON official website, see http://json.org/) in Javascript, the parsing process is quite direct, all you have to do is pass the JSON string to the Javascript eval function. If there is a jsonstring variable containing JSON data, the following code will parse the variable and return the corresponding JavaScript Object:

Note: JSON data should be enclosed in brackets before calling eval. In this way, Eval is required to take the parameter as an expression, instead of interpreting the object literal {} as a code block. However, the eval function can execute arbitrary code. If the data comes from an untrusted source, security problems may occur. For this reason, we recommend that you verify JSON data before calling the eval function.

The official JSON website (http://json.org/) provides a regular expression for verifying JSON data. You can find this regular expression in the Javascript implementation downloaded from this website.

Microsoft Ajax library has its own javascriptserializer object, which is included in the SYS. serialization namespace. This object is responsible for JSON data encoding and decoding. Let's see how it works.

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.