Ajax embracing JSON lets XML walk away!

Source: Internet
Author: User
Tags format define array object eval execution net string
After all, Ajax (Asynchronous JavaScript and XML) is a technology for browsers to asynchronously read XML content on the server. The current technology is usually related to XML, plus a concept as a cover, just like gold, it can't be pulled. The people outside the door looked very lively, and the people inside the door shook their heads. What about XML, a cross-platform trending language? Actually XML = TXT. XML is just text that conforms to many specifications. It is nothing but a file that holds characters. The browser only reads the text content on the server asynchronously, so you don't have to stick to XML when developing Ajax. [Copyright, www.jialing.net]

Origin of JSON

XML is used to format data content. Is there any better way if we don't use XML? This answer is JSON. Before introducing JSON, let me introduce JavaScript as a scripting language. The scripting language itself has the talent for dynamic execution. That is, we can put the statement we want to execute in a string and execute it through the dynamic execution function eval (). The contents of 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>

Open the page and a hello window will pop up.

We can put any script statement in the string, including the statement 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 statement, isn't it an eval method that can be parsed? For parsing complex XML, how much such efficiency is improved!

Let me tell you what is JSON: JavaScript Object Notation. I prefer to translate it into JavaScript object declarations. For example, if you want to load some contacts information from the background, if it is written as XML, it is as follows:

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

Write it as JSON:

[
{
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"
}
]

It's not just about expression. The most important thing is that you can discard the DOM parsing that makes people feel confused. Because as long as it conforms to the JavaScript declaration specification, JavaScrip will automatically parse it for you. The basic method of using JSON in Ajax is to load a string that declares a JavaScript object in the foreground, use the eval method to turn it into an actual object, and finally update the page information through DHTML.
JSON format

The basic format of JSON is as follows, the picture is from json.org:

• An object is a collection of attribute and value pairs. An object starts at "{" and ends at "}". Each attribute name and value are separated by a ":" hint, and attributes are separated by ",".

Ajax embraces json and lets XML go away! -Ajax xml json

数组 Array is a collection of ordered values. An array starts with "[" and ends with "]", with values separated by ",".

Ajax embraces json and lets XML go away! -Ajax json passing xml

· Values can be strings, numbers, true, false, null in quotes, or objects or arrays. These structures can be nested.

Ajax embraces json and lets XML go away! -Ajax json

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

Ajax embraces json and lets XML go away! -Jquery ajax json

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

Ajax embraces json and lets XML go away! -Ajax requests json data


JSON vs XML

· Readability

The readability of JSON and XML is comparable. One side is the recommended syntax and the other is the standard tag form. It is difficult to distinguish the winner.

· Scalability

XML is inherently very extensible, and of course JSON, there is nothing that XML can extend, JSON cannot.

· Coding difficulty

XML has a wealth of encoding tools, such as Dom4j, JDom, etc. JSON also has tools provided by json.org, but JSON encoding is significantly easier than XML. You can write JSON code without the help of tools. It's not easy.

· Decoding difficulty

XML parsing must consider the parent node of the child node, which makes people dizzy, and the difficulty of parsing JSON is almost 0. This XML is really nothing to lose.

  ·Popularity

XML has been widely used in the industry, and JSON has just begun, but in the specific field of Ajax, the future development must be XML giving way to JSON. By then Ajax should become Ajaj (Asynchronous JavaScript and JSON).

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.