A comparative analysis of the advantages and disadvantages of JSON and XML _json

Source: Internet
Author: User
Tags json php server readable

1. Introduction to the definition

1.1 XML definition

The Extensible Markup Language (extensible Markup Language, XML), which is used to mark an electronic file with a structured markup language, can be used to mark data, define data types, and is a source language that allows users to define their own markup language. XML organizes data using the DTD (document type definition) document types definition, which has long been recognized by the industry as a standard for consistency, cross-platform, and language.
XML is a subset of standard Universal Markup Language (SGML) that is ideal for Web transport. XML provides a uniform way to describe and exchange structured data that is independent of applications or vendors.

1.2 JSON definition

JSON (JavaScript Object notation) a lightweight data interchange format with good readability and fast-coding features. Data exchange can be made between different platforms. JSON is highly compatible, completely independent of the language text format, but also has similar C-language habits (including C, C + +, C #, Java, JavaScript, Perl, Python, etc.) the behavior of the system. These features make JSON an ideal data exchange language.
JSON is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999.

2. XML and JSON pros and cons

Advantages and disadvantages of 2.1 xml

The benefits of XML
A. Format uniformity and compliance with standards;
B. Easy to interact with other systems remotely, and data sharing is more convenient.

Disadvantages of XML
A.xml file is large, the file format is complex, the transmission occupies the bandwidth;
B. Both the server side and the client require a large amount of code to parse the XML, causing the server-side and client code to become unusually complex and difficult to maintain;
C. The client is inconsistent in parsing XML between different browsers and needs to write a lot of code repeatedly;
D. Server-side and client-resolution XML cost more resources and time.

Advantages and disadvantages of 2.2 json

The benefits of JSON:

A. The data format is relatively simple, easy to read and write, the format is compressed, occupy a small bandwidth;
B. Easy to parse, client JavaScript can simply read JSON data via eval ();
C. Support multiple languages, including ActionScript, C, C #, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other server-side languages, easy server-side resolution;
D. In the PHP world, there have been Php-json and json-php appeared, biased in PHP after the serialization of the program direct call, PHP server-side objects, arrays, etc. can directly generate JSON format, easy access to the client to extract;
E. Because the JSON format can be used directly for server-side code, it greatly simplifies the amount of code development on both the server side and the client, and completes the task without change and is easy to maintain.

The disadvantage of JSON

A. There is no XML format so popularized and popular, and not as universal as XML;
The B.json format is currently promoted in the Web service and is still in its infancy.

3. XML and JSON compare advantages and disadvantages

Readability aspect.

JSON and XML are basically the same readability of data, and JSON and XML are equally readable, with the suggested syntax on one side, the canonical label form, and the more readable XML.

Scalability aspects.

XML is inherently extensible, of course, and there is no XML that can be extended, JSON cannot.

Coding difficulty aspect.

XML has a wealth of coding tools, such as dom4j, Jdom and so on, JSON also has the tools provided by json.org, but the JSON encoding is significantly easier than XML, even if you can write JSON code without the help of tools, it's not easy to write XML.

Decoding difficulty aspect.

The parsing of XML takes into account the parent node of the child node, making people dizzy, and JSON parsing is almost 0. This XML loser is really nothing to say.

Popularity aspect.

XML has been widely used in the industry, and JSON is only just beginning, but in this particular area of Ajax, the future development must be XML to be in JSON. Ajax should then become Ajaj (asynchronous Javascript and JSON).

means of analysis.

JSON and XML also have rich parsing tools.

Data volume aspect.

JSON is relatively small in size and faster to deliver than XML.

Data interaction aspects.

JSON is more convenient to interact with JavaScript, easier to parse, and better for data to interact with.

Data description aspect.

JSON has less descriptive data than XML.

Transmission speed aspect.

JSON is much faster than XML.

4.XML and JSON data format comparison

4.1 About lightweight and heavyweight

Lightweight and heavyweight are relative, so what is the weight of XML relative to JSON? It should be reflected in parsing, where XML is currently designed with two parsing methods: Dom and SAX.

Dom

Dom is a data Interchange Format XML as a DOM object, you need to read the XML file into memory, which is the same as the principle of JSON and XML, but XML to consider the parent node and child nodes, this point JSON parsing is much less difficult, 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

Sax is a step-by-step method of parsing the parsed content without the need for the entire document to be read. The program can also terminate parsing at any time. In this way, a large document can be gradually, 1.1 points of display, so sax is suitable for large-scale parsing. At this point, JSON is not going to make it.
So the light/weight difference between JSON and XML is:
JSON only provides an overall resolution scheme, and this method only works well when parsing less data;
XML provides a step-by-step solution to large-scale data, which is ideal for processing large amounts of data.

4.2 On data format coding and parsing difficulty

In terms of coding.

While XML and JSON have their own coding tools, JSON is simpler to encode than XML, and can write JSON code without a tool, but writing good XML code is a bit difficult; like XML, JSON is text-based, and they all use Unicode encoding, And it has the same data Interchange Format XML as

Readability.

In subjective terms, JSON is clearer and less redundant. The JSON Web site provides a rigorous description of the JSON syntax, but only a brief description. In general, XML is better suited for tagging documents, while JSON is more suitable for data exchange processing.

In terms of parsing.

In common Web applications, developers often worry about parsing XML, whether it's generating or processing XML on the server side or parsing XML with JavaScript by the client, which often leads to complex code and very low development efficiency.
In fact, for most Web applications, they do not need complex XML to transmit data, and the extensibility of XML claims is rarely advantageous here, and many AJAX applications even return HTML fragments directly to build dynamic Web pages. As opposed to returning XML and parsing it, returning HTML fragments greatly reduces the complexity of the system, but at the same time lacks some flexibility. Data Interchange Format JSON provides better simplicity and flexibility than XML or HTML fragments. XML is still an unshakable place in Web Serivice applications, at least for the time being.

Instance comparison

Both XML and JSON use structured methods to mark data, and here's a simple comparison.
Some provinces and cities in China are represented by XML as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<country>
 <name> China </name>
 < province>
  <name> Heilongjiang </name>
  <citys>
   <city> Harbin </city>
   <city > Daqing </city>
  </citys>
 </province>
 <province>
  <name> Guangdong </ name>
  <citys>
   <city> Guangzhou </city>
   <city> shenzhen </city>
   <city > Zhuhai </city>
  </citys>
 </province>
 <province>
  <name> Taiwan </ name>
  <citys>
   <city> Taipei </city>
   <city> Kaohsiung </city>
  </citys >
 </province>
 <province>
  <name> Xinjiang </name>
  <citys>
   <city> Urumqi </city>
  </citys>
 </province>
</country>

In JSON, some provinces and cities in China are as follows:

 var country =
    {
      name: "China",
      provinces: [
      {name: "Heilongjiang", Citys: {city: ["Harbin", "Daqing"]}},
      {name: "Guangdong", Citys: {city: ["Guangzhou", "Shenzhen", "Zhuhai"]},
      {name: "Taiwan", Citys: {city: ["Taipei", "Kaohsiung"]}},
      {name: "Xinjiang", Citys: {city: ["Urumqi"]} }
      ]
    }

In terms of readability, XML has a distinct advantage, after all, human language is closer to such a descriptive structure. JSON reads more like a block of data, and it's a bit confusing to read. The language we read, however, is precisely suitable for machine reading, so it is possible to read the value of "Heilongjiang" through the index of JSON Country.provinces[0].name.

In the handwriting difficulty of coding, XML is still more comfortable, so it is good to read and write. However, the character JSON written out is significantly less. When you remove blank tables and line-wrapping, JSON is a dense, useful data, while XML contains many repetitive markup characters.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.