Another pattern of data passing in Ajax JavaScript Object notation Thought (JSON) _ajax related

Source: Internet
Author: User
Tags closing tag comments advantage
Ajax is known as the abbreviation for "Asynchronous JavaScript and XML," But while XML is an important part of the look, it is not necessary. A senior software engineer, Douglas Crock Ford, developed a data format built into JavaScript, called a JavaScript Object representation (Json,javascript object notation), It means using Ajax objects directly to pass information and read "Jason."
1. What is JSON
The JSON concept is simple, and JSON is a lightweight data format based on a subset of JavaScript syntax, arrays and object representations. Because JavaScript syntax is used, JSON definitions can be included in JavaScript files, and access to them does not need to be parsed in an xml-based language. Before using JSON, however, it is important to understand the special syntax for arrays and object literals in JavaScript.
1.1 Literal amount of the array
The literal amount of an array is a comma-separated set of JavaScript values enclosed in a single pair of parentheses, for example:
Copy Code code as follows:

var anames=["Hello", A, true, NULL];
[HTML]
1.2 Object literal
The object literal is defined by two curly braces. You can place any number of "name-value" pairs within the curly braces to define the format string values. In addition to the last line, each name-value pair must have a comma (this is similar to the definition of a federated array in Perl). For example:
[Code]
var Ocar = {
"Color": "Red",
"Doors": 4,
' Paidfor ': true
};

1.3 Mixed literal volume
We can mix objects and arrays literally to create an array of objects, or an object that contains an array. For example:
Copy Code code as follows:

{comments:[
{
Id:1,
Author: "Someone1",
URL: "Http://someone1.x2design.net",
Content: "Hello"
},
{
Id:2,
Author: "Someone2",
URL: "Http://someone2.x2design.net",
Content: "Hello"
},
{
Id:3,
Author: "Someone3",
URL: "Http://someone3.x2design.net",
Content: "Hello"
}
]};

1.4 JSON Syntax
In AJAX applications, the server generates JavaScript statements directly, and the client acquires the object directly using the Eval method, which eliminates the ability to parse XML. Also, the advantage of using JSON as a data format in JavaScript communication is that you can get the value of the data immediately, so you can access the data it contains more quickly.
var ocarinfo = eval ("+ Sjson +"));
Keep in mind that curly braces are also a statement in JavaScript. The only way for the parser to know that the curly braces represent an object, not a statement, is to find the parentheses that encapsulate it (it's used to indicate that the code is an expression, not a statement).
1.5 JSON encoding and decoding
As part of the JSON resource, Corockford developed a tool that enables direct decoding and encoding of JSON and JavaScript objects. The source program for this tool can be downloaded in www.crockford.com/JSON/json.js.
There are some inherent drawbacks to using eval () on the above: it is used to evaluate any incoming Javascript code, not just JSON. Therefore, when it comes to enterprise-level Web application development, it has a lot of security implications. To solve this problem, you can use the parser Json.parse () method that is used only to convert the JSON code to Javascript. For example:
var oobject = Json.parse (Sjson);
It also provides a tool for converting JavaScript objects to JSON strings (which are used in data transfer) (not built in JavaScript to support this feature). All you have to do is pass the object to JSON. Stringify () method. Take a look at the following example:
Copy Code code as follows:

var ocar = new Object ();
Ocar.doors = 4;
Ocar.color = "Blue";
Ocar.year = 1995;
Ocar.drivers = new Array ("Penny", "Dan", "Kris");
document.write (Json.stringify (Ocar));

This code will output the JSON string shown below:
{"Doors": 4, "Color": "Blue", "Year": 1995, "Drivers": ["Penny", "Dan", "Kris"]}


2. JSON and XML
As mentioned above, one of the great advantages of JSON versus XML is that it's simpler.
See XML Data Representation instance:
Using XML to represent:
Copy Code code as follows:

<comments>
<comment>
<id>1</id>
<author>someone1</author>
<url>http://someone1.x2design.net</url>
<content>hello</content>
</comment>
<comment>
<id>2</id>
<author>someone2</author>
<url>http://someone2.x2design.net</url>
<content>someone1</content>
</comment>
<comment>
<id>3</id>
<author>someone3</author>
<url>http://someone3.x2design.net</url>
<content>hello</content>
</comment>
</comments>

Use JSON to represent:
Copy Code code as follows:

{comments:[
{
Id:1,
Author: "Someone1",
URL: "Http://someone1.x2design.net",
Content: "Hello"
},
{
Id:2,
Author: "Someone2",
URL: "Http://someone2.x2design.net",
Content: "Hello"
},
{
Id:3,
Author: "Someone3",
URL: "Http://someone3.x2design.net",
Content: "Hello"
}
]};

It's easy to see that a lot of redundant information is missing. Because there is no need to have an end tag (closing tag) that matches the start tag (opening tag), the number of bytes required to transfer the same information is greatly reduced. Founder Corockford called it the "XML Weight-loss program".
The disadvantage of JSON-formatted data compared to XML is that it is less readable for laymen. There is, of course, a view that data interchange formats are not visually observable. If you create and parse data that is transmitted back and forth through tools, there is no reason to require that data be easy to read. The essence of the problem is that there are available JSON tools.

3. Server-side JSON tools
Java:java JSON Tool, developed by Douglas Crock Ford, can be used in www.crockford.com/JSON/java/
, which can be used in JSP.

4. JSON Advantages and Disadvantages
Not only does JSON reduce the performance and compatibility problems of parsing XML parsing, but it's easy to use for JavaScript, it's easy to get data by traversing arrays and accessing object properties, and it's also good for readability, with the basic properties of structured data. Have to say is a good way, and in fact, Google Maps did not use XML to pass data, but the use of JSON scheme.

Another advantage of JSON is cross-domain feasibility, such as your use on www.xxx.com Web pages, which means you can pass information across domains. Using XMLHttpRequest does not capture cross-domain information, which is restricted by the security nature of JavaScript.

JSON looks beautiful, does it completely replace XML? This is not true, and the reason is the advantages of XML: versatility. It is not easy to produce syntactically qualified JavaScript code on the server side, which occurs primarily in a larger system, with different developers on the server side and the client. They must negotiate the format of the object, which can easily cause errors.

At any rate, JSON is an enticing technique, ready to do a lot of probation. Hopefully, you'll get a big performance boost.
Related Article

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.