Json-related technologies

Source: Internet
Author: User

  AJAXThat is, "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML) will be changed to Ajaj (Asynchronous JavaScript and object notation), which is hard to say !!

When it comes to JSon, xml should be mentioned. XML is the standard method for data exchange. This format is used to represent the data exchange payload between the client and the server, and 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.

In AJAX technology, while the user views and interacts with the page (which corresponds to the "Asynchronous" section in AJAX ), javaScript on the page is responsible for sending data requests to a Web server. These requests are just some common HTTP requests, which are exactly the same as the HTTP requests used by the browser for page retrieval (together with any images, stacked Sample tables, and other content. At the same time, the XMLHttpRequest object can be used to retrieve any type of data, not just XML type. For example, JavaScript can use XMLHttpRequest to retrieve a plain text from a Web server and display its content in a form. By looking for the "content-type" header located before the data, the XMLHttpRequest object analyzes the MIME type of the data returned from the Web server. For example, if the MIME type of the data is "text/plain", you can access it by analyzing the responseText attribute of the XMLHttpRequest object. However, if its MIME type is "text/xml", the XMLHttpRequest object must take additional measures: it runs an XML analyzer on the returned Document Object and constructs a Document Object Model (DOM) tree in the memory to describe the document, and also makes it available for the responseXML attribute. Then, you can use the standard DOM method of JavaScript to navigate the tree and retrieve elements, attributes, and other text in the DOM tree. Although XML is the standard method for data exchange, it is usually not the best method. Although XML can add structures and metadata to data, it uses a rather tedious method. XML also has a relatively complex syntax. Therefore, a analyzer is required to analyze the syntax. In JavaScript, XML must be analyzed into a DOM tree for future use. In addition, once you build this DOM tree, you must also navigate to it to create the corresponding JavaScript Object or use XML data in your client Web application in other ways. Here, you will experience 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.

 

<?xml version='1.0' encoding='UTF-8'?>
<card>
<fullname>Sean Kelly</fullname>
<org>SK Consulting</org>
<emailaddrs>
<address type='work'>http://www.cnblogs.com/smallcup/</address>
<address type='home' pref='1'>http://www.cnblogs.com/smallcup/</address>
</emailaddrs>
<telephones>
<tel type='work' pref='1'>+1 214 555 1212</tel>
<tel type='fax'>+1 214 555 1213</tel>
<tel type='mobile'>+1 214 555 1214</tel>
</telephones>
<addresses>
<address type='work' format='us'>1234 Main St
Springfield, TX 78080-1216</address>
<address type='home' format='us'>5678 Main St
Springfield, TX 78080-1316</address>
</addresses>
<urls>
<address type='work'>http://www.cnblogs.com/smallcup/</address>
<address type='home'>http://www.cnblogs.com/smallcup/</address>
</urls>
</card>

In JSON format, the above format will become the following:

{
"fullname": "Sean Kelly",
"org": "SK Consulting",
"emailaddrs": [
{"type": "work", "value":http://www.cnblogs.com/smallcup/"},
{"type": "home", "pref": 1, "value": "http://www.cnblogs.com/smallcup/"}
],
"teleph [
{"type": "work", "pref": 1, "value": "+1 214 555 1212"},
{"type": "fax", "value": "+1 214 555 1213"},
{"type": "mobile", "value": "+1 214 555 1214"}
],
"addresses": [
{"type": "work", "format": "us",
"value": "1234 Main StnSpringfield, TX 78080-1216"},
{"type": "home", "format": "us",
"value": "5678 Main StnSpringfield, TX 78080-1316"}
],
"urls": [
{"type": "work", "value": "http://www.cnblogs.com/smallcup/"},
{"type": "home", "value": "http://www.cnblogs.com/smallcup/"}
]
}

As you can see, JSON also provides a structure with nested data elements, just like XML. Like XML, JSON is also a base
And both use Unicode encoding, and are as readable as XML. Subjectively, JSON is clearer and redundant. The JSON website provides the JSON syntax
The description is brief. In general, XML is more suitable for marking documents, while JSON is more suitable for data exchange processing. Every instance in a JSON document describes an object. The specific description is implemented by using nested objects, arrays, strings, numbers, Boolean values, or null values.

The degree of balance between the two is compared below:

  • The readability of JSON and XML is comparable. One side is the suggested syntax and the other side is the standard label form, which makes it difficult to distinguish between the two.
  • Scalable XML is inherently highly scalable, and JSON is also available. Nothing is XML, and JSON cannot.
  • Encoding difficulty XML has a variety of encoding tools, such as Dom4j and JDom. JSON also provides tools provided by json.org, but JSON encoding is much easier than XML.
  • Decoding difficulty XML parsing should consider the parent node of the child node, which is confusing, while the JSON parsing difficulty is almost 0. There is nothing to say about XML.
  • Popular XML has been widely used in the industry, and JSON is just the beginning, but it is in the specific field of Ajax.

The basic idea of JSON is to implement itself as JavaScript (and the main replacement for XML data operations in Pyth 2.0 development ). Any developer, whether developing standard desktop applications or Web applications, will enjoy the simple features of JSON as long as XML data processing is used. Finally, I hope you can better understand how to use Json.

(Note: This article is written by the author through his own learning and summary of his predecessors)

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.