Ajax is the abbreviation of "Asynchronous javascript and XML", but although XML is an important component, it is not necessary. Douglas Crock ford, a senior software engineer, developed a javascript-based data format called javascript Object Representation (JSON, javascript Object Notation ), it means that the Ajax object is used directly to transmit information and can be read as "Jason ".
1. What is JSON
JSON is a lightweight data format based on a subset of javascript syntax, that is, array and object representation. Because javascript syntax is used, JSON definitions can be included in javascript files, and access to them does not need to be parsed in XML-based languages. However, before using JSON, it is very important to understand the special Syntax of arrays and object literally in javascript.
1.1 array literal
Array literal, which is a set of javascript values separated by commas (,) enclosed by square brackets. For example:
Copy codeThe Code is as follows:
Var aNames = ["hello", 12, 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 in curly brackets to define the lattice string value ". Except the last line, each "name-value" pair must be followed by a comma (which is similar to the definition of the Union array in Perl ). For example:
[Code]
Var oCar = {
"Color": "red ",
"Doors": 4,
"PaidFor": true
};
1.3 mixed literal
We can mix objects and array literal values to create an array of objects or an object containing arrays. For example:
Copy codeThe Code is 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 directly generates javascript statements. After the client obtains the object, the eval method is used to obtain the object, saving the performance loss of parsing XML. At the same time, the advantage of using JSON as the data format in javascript communication is that you can immediately obtain the data value, so you can access the data contained in it faster.
Var oCarInfo = eval ("(" + sJSON + ")");
Remember: curly braces in javascript are also a statement. The only way for the parser to know that the curly braces represent an object rather than a statement is to find the parentheses that encapsulate it (it is used to indicate that the code is an expression rather than statement ).
1.5 JSON encoding and decoding
As part of the JSON resource, Corockford developed a tool that can directly decode and encode JSON and Javascript objects. The source program of this tool can be downloaded from www.crockford.com/JSON/json.js.
As mentioned above, eval () is used to evaluate any Javascript code passed in, not just JSON. Therefore, when enterprise-level web application development is involved, it has great security risks. To solve this problem, you can use the JSON. parse () method, a parser that only converts JSON code to Javascript. For example:
Var oObject = JSON. parse (sJSON );
At the same time, it also provides a tool to convert Javascript objects into JSON strings (used during data transmission) (this feature is not supported in Javascript ). All you need to do is pass the object to the JSON. Stringify () method. See the following example:
Copy codeThe Code is 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 following JSON string:
{"Doors": 4, "color": "blue", "year": 1995, "drivers": ["Penny", "Dan", "Kris"]}
2. JSON and XML
As mentioned above, JSON is simpler than XML.
See XML data to indicate instances:
In XML format:
Copy codeThe Code is 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>
JSON format:
Copy codeThe Code is 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 is easy to find that many redundant information is missing. Because there is no need for a closing tag that matches the opening tag, the number of bytes required to transmit the same information is greatly reduced. Corockford, founder, calls it "XML weight loss solutions ").
The disadvantage of JSON-format data compared with XML is that it is less readable for outsiders. Of course, there is a point of view that the data exchange format is not observed by the naked eye. If a tool is used to create and parse the data transmitted back and forth, there is no reason to make the data easy to read. The essence of the problem is that there are available JSON tools.
3. server-side JSON Tool
Java: java JSON tool, developed by Douglas Crock ford, available at www.crockford.com/JSON/java/
. It can be used in JSP.
4. Advantages and Disadvantages of JSON
JSON not only reduces the performance and compatibility issues caused by parsing XML parsing, but also is very easy to use for javascript. It is convenient to retrieve data by traversing arrays and accessing object attributes, its readability is good, and it basically has the nature of structured data. I have to say that it is a good method. In fact, google maps does not use XML to transmit data, but uses the JSON scheme.
Another advantage of JSON is cross-origin feasibility. For example, it is completely feasible for you to use it on www.xxx.com, which means you can transmit information across domains. However, XMLHttpRequest cannot obtain cross-origin information, which is restricted by the security nature of javascript.
JSON looks pretty, can it completely replace XML? This is not the case because of the advantages of XML: versatility. It is not easy for the server to generate javascript code with the correct syntax. This mainly happens in a large system, where different developers are working on the server and the client. They must negotiate the format of the object, which can easily cause errors.
In any case, JSON is an attractive technology and is ready for a large number of trials. We hope that the performance will be improved significantly.