Ajax: Embracing JSON and letting XML walk away

Source: Internet
Author: User
Tags format define array object eval execution net string
Ajax|js|xml

Ajax (Asynchronous JavaScript and XML) is essentially a technique by which browsers asynchronously read XML content on a server. Now the technology is usually related to XML, plus a concept as a cover, like gold installed, pull not. The people outside the door were very lively, and the people in the door shook their heads. What about XML, a cross-platform, trendy language? Actually xml=txt. XML is just text that conforms to many specifications. It itself is nothing but a file that saves characters. While the browser asynchronously reads only the text content on the server, there is no need to adhere to XML at all when developing Ajax. [All rights reserved, Www.jialing.net]

The origins of JSON

The role of XML is to format the content of the data. What better way to do it if we don't use XML? The answer is JSON. Before I introduce JSON, let me introduce the scripting language of JavaScript. The scripting language itself has the gift of dynamic execution. That is, we can put the statement we want to execute in a string and execute it through the dynamic execution function of eval (). The contents of the string are executed as we write the script.

Example 1:



Eval Example 1




Opening the page will pop up the Hello window.

We can put any script statements in the string, including declaration statements:



Eval Example 2




If the text we came asynchronously in the background was a JavaScript declaration statement, wouldn't an eval method parse it? How much more efficient it is to parse complex XML!

Now let's tell you what a json:javascript Object notation is. I'd rather translate it into a JavaScript object declaration. For example, to load some address book from the background information, if written in XML, as follows:



Michael
17bity@gmail.com
http://www.jialing.net


John
john@gmail.com
http://www.john.com


Peggy
peggy@gmail.com
http://www.peggy.com

And it's written in 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"
}
]

The simple is not just expression, the most important thing is to discard the confusing DOM parsing. Because as long as the JavaScript-compliant declaration specification, Javascrip will automatically help you parse the good. The basic way to use JSON in Ajax is for the foreground to load a string for the background declaration of a JavaScript object, to use the Eval method to convert it to the actual object, and finally to update the page information through DHTML.
  Format of JSON

The basic format of JSON is as follows, with pictures from json.org:

• An object is a collection of properties, value pairs. The beginning of an object is "{", Ending with "}". Each property name and value is separated by the ":" Prompt, and the attributes are delimited by ",".



• An array is a collection of sequential values. An array begins with "[", Ends with "]", and the values are separated by ",".



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



• The definition of a string is basically consistent with C or Java.



• The definition of numbers is also basically consistent with C or Java.



JSON VS XML

• readability

JSON and XML are comparable in readability, with the suggested syntax on one side and the canonical label form, which is difficult to distinguish between the winners and losers.

• Scalability

XML is inherently extensible, of course, and there is no XML that can be extended, JSON cannot.

• Coding Difficulty

XML has a wealth of coding tools, such as dom4j, Jdom and so on, JSON also has the tools provided by json.org, but the JSON encoding is significantly easier than XML, even if you can write JSON code without the help of tools, it's not easy to write XML.

• Decoding difficulty

The parsing of XML takes into account the parent node of the child node, making people dizzy, and JSON parsing is almost 0. This XML loser is really nothing to say.

• Popular Degree

XML has been widely used in the industry, and JSON is only just beginning, but in this particular area of Ajax, the future development must be XML to be in JSON. Ajax should then 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.