Ajax: Embrace JSON and let XML go away

Source: Internet
Author: User
Copyright. For more information, see www.jialing.net.

Ajax (Asynchronous JavaScript and XML) is a technology in which browsers asynchronously read XML content from servers. The current technology is related to XML, and the concept is used as a joke, just like a gold outfit. The people outside the door were very busy, but the people inside the door shook their heads. What about XML? In fact, XML = TXT. XML is just a text that conforms to many specifications. It is nothing, but a file that saves characters. The browser asynchronously reads only the text content on the server, so you do not have to stick to XML during Ajax development. [Copyright, www.jialing.net]


  • The origin of JSON

XML is used to format data. Is there any better way if we don't need XML? The answer is JSON. Before introducing JSON, I would like to introduce the JavaScript script language. The scripting language has its own talent for dynamic execution. That is, we can place the statement we want to execute in the string and execute it through the dynamic execution function eval. The content in the string will be executed like the script we wrote.
Example 1: <HTML>
<HEAD>
<TITLE> eval example 1 </TITLE>
</HEAD>
<BODY>
<Script>
Str = "alert ('hello ')";
Eval (str );
</Script>
</BODY>
</HTML>

The hello window is displayed.
We can put any script statement in the string, including the declaration statement: <HTML>
<HEAD>
<TITLE> eval example 2 </TITLE>
</HEAD>
<BODY>
<Script>
Define = "{name: 'Michael ', email: '17bity @ gmail.com '}";
Eval ("data =" + define );
Alert ("name:" + data. name );
Alert ("email:" + data. email );
</Script>
</BODY>
</HTML>

If the text that we send asynchronously in the background is a JavaScript declaration statement, can it be parsed without an eval method? How efficient is this process for parsing complex XML!
Now let's tell you what JSON: JavaScript Object Notation is. I prefer to translate it into JavaScript Object declaration. For example, to load some address book information from the background, if it is written as XML, as follows:

<Contact>
<Friend>
<Name> Michael </name>
<Email> 17bity@gmail.com </email>
<Homepage> http://www.jialing.net </Friend>
<Friend>
<Name> John </name>
<Email> john@gmail.com </email>
<Homepage> http://www.john.com </Friend>
<Friend>
<Name> Peggy </name>
<Email> peggy@gmail.com </email>
<Homepage> http://www.peggy.com </Friend>
</Contact>

In JSON format :[
{
Name: "Michael ",
Email: 17bity@gmail.com ",
Homepage: "http://www.jialing.net"
},
{
Name: "John ",
Email: john@gmail.com ",
Homepage: "http://www.jobn.com"
},
{
Name: "Peggy ",
Email: peggy@gmail.com ",
Homepage: "http://www.peggy.com"
}
]

The simplest form is not just expression, but the most important thing is to discard the confusing DOM parsing. As long as it complies with the JavaScript statement specification, commit Crip will automatically resolve it for you. The basic method to use JSON in Ajax is to load the string of the JavaScript Object declared in the background at the front end, convert it to the actual object using the eval method, and update the page information through DHTML.

  • JSON format

The basic JSON format is as follows:
An object is a set of attributes and value pairs. An object starts with "{" and ends "}". Each attribute name and value is separated by a ":" prompt, and.

An array is a set of ordered values. An array starts with "[", ends with "]", and values are separated.

The value can be a string, number, true, false, or null in quotation marks, or an object or array. These structures can be nested.

The definition of a string is basically the same as that of C or Java.

The definition of numbers is also basically the same as that of C or Java.

  • JSON VS XML

Readability
The readability of JSON and XML is comparable. One side is the suggested syntax and the other side is the standard tag format, which makes it difficult to distinguish between the two.

Scalability

XML is inherently highly scalable, and JSON is also available. There is nothing XML can be extended, and JSON cannot.

Encoding difficulty
XML has rich coding tools, such as Dom4j and JDom, and JSON also provides tools provided by json.org. However, JSON encoding is much easier than XML, JSON code can be written without tools, but it is not easy to write XML.

Decoding difficulty
XML parsing takes into account the parent node of the child node, which is confusing, and the difficulty of JSON Parsing is almost 0. There is nothing to say about XML.

Popularity

XML has been widely used in the industry, and JSON is just the beginning. However, in the specific field of Ajax, the future development of XML must be in JSON. Then Ajax should be changed to Ajaj (Asynchronous JavaScript and JSON.

[Copyright, www.jialing.net]

Appendix:
JSON.org: http://www.json.org/
JSON in JavaScript: http://www.json.org/js.html
JSON: The Fat-Free Alternative to XML: http://www.json.org/xml.html
JSON and the Dynamic Script Tag: Easy, XML-less Web Services for JavaScript: http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html
Using JSON (JavaScript Object Notation) with Yahoo! Web Services: http://developer.yahoo.com/common/json.html

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.