Introduction to the advantages and disadvantages of JSON and XML

Source: Internet
Author: User
JSON (JavascriptObjectNotation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavascriptProgrammingLanguage and StandardECMA2623rdEditionDecember1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C ,... JSON (Javascript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of Javascript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, Javascript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.
The JSON concept is very simple, that is, the server directly generates Javascript statements, and the client directly uses the eval method to obtain this object after obtaining the object, which can save the parsing XML loss.
To load information from the background and write it as XML, as follows:

<contact><friend><name>Michael</name><email>17bity@gmail.com</email><homepage>http://www.jialing.net</homepage></friend><friend><name>John</name><email>john@gmail.com</email><homepage>http://www.john.com</homepage></friend><friend><name>Peggy</name><email>peggy@gmail.com</email><homepage>http://www.peggy.com</homepage></friend></contact>

JSON format:

[{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.

JSON not only reduces the performance and compatibility issues caused by parsing XML parsing, but also is very easy to use for Javascript. it is convenient to retrieve data by traversing arrays and accessing object attributes, its readability is good, and it basically has the nature of structured data. I have to say that it is a good method. In fact, google maps does not use XML to transmit data, but uses the JSON scheme.

Another advantage of JSON is "cross-origin". for example, you can use

JSON definition

A lightweight data exchange format with good readability and ease of writing. Mainstream technologies in the industry provide a complete solution (similar to regular expressions and supported by most languages) to exchange data between different platforms. JSON adopts a highly compatible text format and has behaviors similar to the C language system.

XML definition

Extensible Markup Language (XML) is used to mark electronic files so that they have a structured Markup Language. it can be used to mark data and define data types, is a source language that allows you to define your own markup language. XML is a subset of the standard general Markup Language (SGML) and is suitable for Web transmission. XML provides a unified way to describe and exchange structured data independent of applications or vendors.

[XML]
Advantages of using XML as the transmission format:

  1. Unified format, compliant with standards

  2. It is easy to remotely interact with other systems, making data sharing easier.

Disadvantages:

  1. XML file format is large, complex, and bandwidth used for transmission

  2. Both the server and client require a large amount of code to parse XML, regardless of the complexity and difficulty of maintaining the server and client code.

  3. The XML parsing methods for different browsers on the client are inconsistent, so you need to repeat a lot of code.

  4. It takes resources and time for the server and client to parse XML

[JSON]
In addition to the XML format, there are no other formats. there is a lightweight data exchange format called JSON (JavaScript Object Notation) that can replace XML.

Advantages:

  1. The data format is relatively simple, easy to read and write, the format is compressed, and the bandwidth usage is small

  2. This language is easy to parse. the client JavaScript can simply read JSON data through eval_r ().

  3. Supports multiple languages, including ActionScript, C, C #, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby, and other language server-side languages for server-side parsing

  4. In the PHP world, there has been PHP-JSON and JSON-PHP appear, so that PHP serialized programs can directly call. PHP server side objects, arrays and so on can directly generate JSON format, convenient client access extraction.

  5. Because the JSON format can be used directly for the server code, it greatly simplifies the code development of the server and client, but the completed tasks remain unchanged and easy to maintain

Disadvantages:

  1. It is widely used and widely used without the XML format.

  2. JSON format is currently in the initial stage for Web Service promotion.
    Comparison of advantages and disadvantages of JSON and XML

  3. In terms of readability, the data readability of JSON and XML is basically the same. The readability of JSON and XML is comparable. one side is the suggested syntax and the other side is the standard tag format, which makes it difficult to distinguish between the two.

  4. In terms of scalability, XML is inherently highly scalable, and JSON is also available. there is nothing XML can be extended, and JSON cannot.

  5. In terms of 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, JSON code can be written without tools, but it is not easy to write XML.

  6. In terms of decoding difficulty, XML parsing must take into account the parent node of the child node, which is confusing, while JSON parsing is almost zero. There is nothing to say about XML.

  7. In terms of popularity, XML has been widely used in the industry, and JSON has just begun. However, in the specific field of Ajax, the future development of XML must be in JSON. Then Ajax should be changed to Ajaj (Asynchronous Javascript and JSON.

  8. JSON and XML have rich parsing methods.

  9. JSON is smaller than XML.

  10. The interaction between JSON and JavaScript is more convenient.

  11. JSON is less descriptive than XML.

  12. JSON is much faster than XML.

1. compare data exchange formats with XML and JSON:
XML: extensible markup language, a language similar to HTML. it has no predefined tags and uses document type definition (DTD) to organize data. the format is uniform, cross-platform and language have long been recognized in the industry. For details, refer to Google or Baidu. Compared with JSON, a lightweight data exchange format, XML can be called heavyweight.
JSON: JavaScript Object Notation is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.
2. Comparison of data exchange formats concerning lightweight and heavyweight:
Lightweight and heavyweight are relative, so what is the significance of XML compared with JSON? I think it should be reflected in parsing. XML currently has two parsing methods: DOM and SAX;
The DOM regards XML as a DOM object in a data exchange format and needs to read the XML file into the memory. in this regard, the principles of JSON and XML are the same, however, XML should take into account the parent and child nodes. JSON parsing is much less difficult, because JSON is constructed in two structures: key/value, a set of key-value pairs; an ordered set of values, which can be understood as an array;
SAX does not need to read the entire document to process the parsed content. it is a step-by-step Parsing method. The program can terminate the resolution at any time. In this way, a large document can be gradually presented at 1.1 points, so SAX is suitable for large-scale parsing. JSON cannot be used at present.
Therefore, the lightweight/heavyweight difference between JSON and XML is that JSON only provides the overall resolution scheme, and this method can only achieve good results when parsing less data; XML provides a step-by-step resolution solution for large-scale data, which is suitable for processing large amounts of data.
3. data exchange format comparison:
In terms of encoding, although both XML and JSON have their own encoding tools, JSON encoding is simpler than XML. even if you do not use the tools, you can write JSON code, however, it is a little difficult to write good XML code. JSON, like XML, is also text-based and uses Unicode encoding, and is as readable as XML in data exchange format.
Subjectively, JSON is clearer and redundant. The JSON website provides a strict description of the JSON syntax, but the description is short. In general, XML is more suitable for marking documents, while JSON is more suitable for data exchange processing.
In terms of parsing, in the common web application field, developers often worry about XML parsing, whether it is generating or processing XML on the server side or parsing XML on the client side using JavaScript, it often leads to complicated code and extremely low development efficiency.
In fact, for most web applications, they do not need complicated XML to transmit data. XML claims that scalability has few advantages here; many Ajax applications even directly return HTML fragments to build dynamic web pages. Compared with returning XML and parsing it, returning HTML fragments greatly reduces the complexity of the system, but at the same time lacks some flexibility. Compared with XML or HTML fragments, the data exchange format JSON provides better simplicity and flexibility. In web serivice applications, XML remains unshakable for the moment.

The above is a detailed description of the advantages and disadvantages of JSON and XML. For more information, see other related articles in the first PHP community!

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.