JSON and XML

Source: Internet
Author: User
XML & mdash; this format is used to represent the data exchange payload between the client and the server. It has almost become a synonym for Webservices. We know that AJAX technology can make every request faster. For each request, not the whole page, but only the data to be returned... SyntaxHighlighter.

XML -- this format is used to represent the data exchange payload between the client and the server. It has almost become a synonym for Web services. We know that AJAX technology can make every request faster. For each request, the returned data is not the whole page, but also the data to be returned. However, the emergence of Ajax and REST technologies affects application architecture, which forces people to seek alternatives to 'xml, such as JavaScript Object Notation (JSON ).

JSON is a lighter and more friendly Web services Client format (mostly in the form of browsers or Ajax applications that access REST-style Web services) it attracted the attention of Web service providers. AJAX usually returns XML format data, and then parses and renders the XML format data through complicated JavaScript scripts on the client. JSON (read Jason) is used to make the data format a standard, which is simpler to be parsed by JavaScript.

This article will explain why JSON is highly respected in Web services design, as well as its main advantages and limitations as an XML alternative. This article will also discuss in depth: how to generate JSON output conveniently in Java Web services with the corresponding Web service client choosing to use JSON.
Advantages of JSON

 


Lightweight Data Exchange Format

Easier reading and writing

Easy machine resolution and generation

JSON parsing using eval () function in JavaScript

JSON supports multiple languages. Including: ActionScript, C, C #, ColdFusion, E, Java, JavaScript, ML, Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Lua.


JSON syntax is a protocol used to transmit and generate data. It is similar to the C language, so it is easily parsed by the C language.
Object: object contains {}
Attribute: the Key-Value pair is used. Use commas to separate attributes. String: value
Array: The array is stored in [] [elements].
Elements: separated by commas
Value: The value can be a string, number, object, array, true, false, or null.

Example:
JSON
{"Menu ":{
"Id": "file ",
"Value": "File :",
"Popup ":{
"Menuitem ":[
{"Value": "New", "onclick": "CreateNewDoc ()"},
{"Value": "Open", "onclick": "OpenDoc ()"},
{"Value": "Close", "onclick": "CloseDoc ()"}]
}
}}


XML








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 will 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.


 Eval example 1


Script
Str = "alert ('hello ')";
Eval (str );
Script

 


The hello window is displayed. We can put any script statement in the string, including the declaration statement, and so on, such:


Eval example 2


Script
Define = "{name: 'Michael ', email: '17bity @ gmail.com '}";
Eval ("data =" + define );
Alert ("name:" + data. name );
Alert ("email:" + data. email );
Script


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:

 
   Michael
   17bity@gmail.com
   Http://www.jialing.net
 
 
   John
   John@gmail.com
   Http://www.john.com
 
 
   Peggy
   Peggy@gmail.com
   Http://www.peggy.com
 


The JSON format is as follows:
[
{
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 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.

 

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.