0. Off-Topic
I believe that any programmer who works with the web, especially the front-end engineers, has no idea what it's all about when it encounters XML for the first time. And then to be arranged with JSON dating, is also confused.
What, wrong? Well, at least, the above is my own experience. In the second project I participated in during my internship, I was asked to use an asynchronous request to fetch a piece of JSON data to the server. Through the browser's console, I look at the results returned by the server, and I remember that moment in my heart exclaimed "What's that crap".
And for asynchronous requests, I only knew how to use the $.ajax () method of the jquery library, and then I tried to write my own Ajax method, and of course, it needed to accumulate experience to become more perfect and easy to use (that is, you, AJAX, electro-optical flint). Ah, I'm sorry, I have to stop at that point.
1. Concept
· Json
JSON (Javascript object Notation,javascript) is a lightweight data format, not a programming language. It uses some patterns in JavaScript to represent structured data, and with the Eval () method, JavaScript can convert this data format to the corresponding JS type.
. Xml
XML (extensible Markup Language, Extensible Markup Language) is a markup language used to exchange data, and the same markup language as HTML (hypertext Markup Language, Hypertext Markup Language) is the markup language used to display the data. It allows users to customize tags to tag data and define data types.
• contact
Both JSON and XML are used across platforms, and they are used for exchanging data. The latter is relatively more historic, so versatility is a natural way to rob, and it derives a lot of other languages, such as SVG (something I'm slowly studying), RSS, and so on.
However, because the JSON format is straightforward and generally compressed, the size of the requested file shrinks naturally. Therefore, front-end engineers should not love it.
2. Syntax
• Variable JSON
If you look at a JSON that is returned from the server, you'll find it's like a literal in JavaScript (literal), which can be any basic data type except undefined (String, number, Boolean, and null). It can also be objects and arrays in complex data types.
For example, there are several JSON formats that may be returned from the server:
"Hello world!"
{Name: "Linji", email: {Personal: ["[Email protected]", "[email protected]"],company: "[email protected]"},learnt: [{ Name: "SVG", Status:0},{name: "JQuery", status:0.5}]}
• Standard XML
The second JSON example above, if written in almost the meaning of the XML, the format is roughly the following
<?xml version= "1.0" encoding= "Utf-8"? ><me><name>linji</name><email><personal >[email Protected]</personal><personal>[email Protected]</personal><company>[email Protected]</company></email><learnt><name>svg</name><status>0</status ></learnt><learnt><name>jquery</name><status>0.5</status></learnt ></me>
3. Pros and cons
· Json
Advantages:
(1) Light weight, small bandwidth requirements when requested
(2) Multiple server languages have modules and methods for creating JSON format for cross-platform use and post-maintenance
Disadvantages:
(1) Without understanding the JSON structure, client-friendly programming is not able to use JSON correctly
· Xml
Advantages:
(1) Unified format, strong extensibility
(2) Strong readability
Disadvantages:
(1) Large file
(2) Client resolution is difficult
(3) Inconvenient maintenance
(4) Inconsistent method of parsing between browsers, resulting in higher code rewrite rate
4. Contrast
(1). Readability.
JSON and XML are basically the same readability of data, JSON and XML are comparable in readability, while the proposed syntax, one side of the canonical label form, XML readability.
(2). Extensibility aspects.
XML is inherently well-extensible, and JSON, of course, has nothing to do with XML extension, not JSON.
(3). Coding difficulty.
XML has a wealth of coding tools, such as dom4j, Jdom, and so on, JSON also has json.org provided tools, but JSON is much easier to encode than XML, even if the use of tools can write JSON code, but to write good XML is not easy.
(4). Decoding difficulty.
Parsing the XML takes into account the child node parent, which makes the person dizzy, and the JSON is almost 0 difficult to parse. This is a little bit of XML output.
(5). degree of popularity.
XML has been widely used in the industry, and JSON has only just begun, but in the specific area of Ajax, the future development must be XML to be located in JSON. Ajax should become Ajaj (asynchronous Javascript and JSON).
(6). Analytical means.
JSON and XML also have rich parsing tools.
(7). Data volume aspect.
JSON, with respect to XML, has a small volume of data and is faster to pass.
(8). Data interaction aspects.
The interaction between JSON and JavaScript is more convenient, easier to parse, and better to interact with the data.
(9). Data description aspects.
JSON is less descriptive of the data than XML.
(10). Transmission speed.
JSON is much faster than XML.
5. Use
· Json
I can only provide front-end engineers with JSON applications. Inconvenience, can not forgive me also reluctantly forgive.
The $.ajax () method enables you to translate the JSON returned from the server directly into the actual JavaScript data type. Its internal principle is that the results returned by the server, through Eval (JSON), the similar literal content in JSON is created, and as a callback method success Arguments[0] passed back to the client, and then within the callback method, It is possible to use this JSON freely as a common JavaScript data type.
Provide an "example" for geek to do their own hands:
Create a file (without a suffix), use the editor to open the file, copy the second example of the JSON above, along with the HTML file with the following code, on the server (native Apache, some Ides, or your own test server. Windows users can use Chrome to make local asynchronous requests, and check their own methods. XXX is the absolute address or relative address of the file that is written with JSON. When the following method executes successfully, "[email protected]" is output on the page. It is not difficult to see, in the Success method, you have been able to operate as a JS object in general, the arbitrary "use" that json.
$.ajax ({ "xxx", "get" , "JSON" ,function (data) { document.write (data.email.personal[1]);} );
· Xml
The use of XML is not discussed here for the time being. It is irresponsible to cite one of the above sentences here.
6. After that
When we know enough about JSON, we can try to understand JSONP. JSONP is a solution that enables your JSON to exchange data across domains.
The XML-derived subset,-svg (Scalable vector graphics, scalable vectors), is a language that generates vector images. In today's development of the retina display, it is absolutely necessary to be able to present a sufficiently clear image at any time.
Reference
SON and XML comparison of advantages and disadvantages _ Rabbit Skin _ Sina Blog
The relationship between JSON and XML