Home > Web Front-end > JS Tutorial > body text

Comparative analysis of the advantages and disadvantages of JSON and XML_json

WBOY
Release: 2016-05-16 15:49:55
Original
1175 people have browsed it

1. Definition introduction

1.1 XML definition

Extensible Markup Language (XML), a markup language used to mark electronic documents to make them structural. It can be used to mark data and define data types. It is a markup language that allows users to define their own. source language. XML uses DTD (document type definition) document type definition to organize data; the format is unified, cross-platform and language, and has long become a recognized standard in the industry.
XML is a subset of Standard Generalized Markup Language (SGML) and is well suited for Web transport. XML provides a unified method for describing and exchanging structured data independent of applications or vendors.

1.2 JSON definition

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and easy to write quickly. Data exchange between different platforms is possible. JSON adopts a highly compatible and completely language-independent text format, and also has behavior similar to C language habits (including C, C, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.
JSON is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999.

2. Advantages and disadvantages of XML and JSON

2.1 Advantages and Disadvantages of XML

Advantages of XML
A. The format is unified and conforms to standards;
B. It is easy to interact remotely with other systems, and data sharing is more convenient.

Disadvantages of XML
A.XML files are huge, the file format is complex, and the transmission consumes bandwidth;
B. Both the server and the client need to spend a lot of code to parse XML, causing the server and client code to become extremely complex and difficult to maintain;
C. The way of parsing XML between different browsers on the client side is inconsistent, and a lot of code needs to be written repeatedly;
D. The server and client spend more resources and time parsing XML.

2.2 Advantages and Disadvantages of JSON

Advantages of JSON:

A. The data format is relatively simple, easy to read and write, the formats are compressed, and takes up little bandwidth;
B. Easy to parse, client-side JavaScript can simply read JSON data through eval();
C. Supports multiple languages, including ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other server-side languages ​​to facilitate server-side parsing;
D. In the PHP world, PHP-JSON and JSON-PHP have already appeared. It is preferable to call the PHP serialized program directly. PHP server-side objects, arrays, etc. can directly generate JSON format to facilitate client access and extraction;
E. Because the JSON format can be used directly by server-side code, it greatly simplifies the code development of server-side and client-side, and the task remains unchanged and is easy to maintain.

Disadvantages of JSON

A. It is not as popular and widely used as XML format, and it is not as versatile as XML;
B. The promotion of JSON format in Web Service is still in its infancy.

3. Comparison of the advantages and disadvantages of XML and JSON

In terms of readability.

The data readability of JSON and XML is basically the same. The readability of JSON and XML is almost the same. On one side is the recommended syntax and on the other side is the standardized tag form. XML is more readable.

In terms of scalability.

XML is naturally very scalable, and JSON certainly has it, too. There is nothing that XML can expand that JSON cannot.

In terms of coding difficulty.

XML has a wealth of encoding tools, such as Dom4j, JDom, etc., and JSON also has tools provided by json.org. However, JSON encoding is obviously much easier than XML. You can write JSON code even without using tools, but you need to write Good XML is not easy.

Decoding difficulty.

The parsing of XML has to consider the child nodes and parent nodes, which makes people dizzy, while the difficulty of parsing JSON is almost 0. XML loses really nothing at this point.

Popularity.

XML has been widely used in the industry, while JSON has just begun. However, in the specific field of Ajax, the future development must be that XML gives way to JSON. By then Ajax should become Ajaj (Asynchronous Javascript and JSON).

In terms of analytical methods.

JSON and XML also have rich parsing methods.

In terms of data volume.

Compared to XML, JSON has a smaller data size and faster transmission speed.

Data interaction.

The interaction between JSON and JavaScript is more convenient, easier to parse and process, and provides better data interaction.

Data description.

JSON is less descriptive of data than XML.

Transmission speed.

JSON is much faster than XML.

4. Comparison of XML and JSON data formats

4.1 About lightweight and heavyweight

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

DOM

DOM regards a data exchange format XML as a DOM object, and the entire XML file needs to be read into the memory. The principles of JSON and XML are the same in this regard, but XML must consider parent nodes and child nodes. JSON is much less difficult to parse because JSON is built on two structures: key/value, a collection of key-value pairs; an ordered collection of values, which can be understood as an array;

SAX

SAX can process the parsed content without reading the entire document. It is a step-by-step parsing method. The program can also terminate parsing at any time. In this way, a large document can be displayed gradually and bit by bit, so SAX is suitable for large-scale parsing. This is currently not possible with JSON.
Therefore, the light/heavyweight difference between JSON and XML is:
JSON only provides an overall parsing solution, and this method can only achieve good results when parsing less data;
XML provides a step-by-step parsing solution for large-scale data, which is very suitable for processing large amounts of data.

4.2 Regarding the difficulty of data format encoding and parsing

In terms of coding.

Although XML and JSON have their own encoding tools, JSON encoding is simpler than XML. You can write JSON code even without the help of tools, but it is a bit difficult to write good XML code; the same as XML , JSON is also text-based, and they all use Unicode encoding, and it has the same characteristics as the data exchange format XML

Readability.

Subjectively speaking, JSON is clearer and less redundant. The JSON website provides a strict, if brief, description of JSON syntax. Generally speaking, XML is more suitable for marking documents, while JSON is more suitable for data exchange processing.

In terms of parsing.

In the field of ordinary web applications, developers often struggle with XML parsing. Whether it is generating or processing XML on the server side, or parsing XML on the client side using JavaScript, it often results in complex code and extremely low development efficiency.
In fact, for most Web applications, they do not need complex XML to transmit data at all. The extensibility claimed by XML rarely has an advantage 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 it also lacks a certain degree of flexibility. The data exchange format JSON provides greater simplicity and flexibility than XML or HTML fragments. In Web Service applications, at least for now, XML still has an unshakable position.

Example comparison

Both XML and JSON use structured methods to mark data. Let’s make a simple comparison below.
The data of some provinces and cities in China are represented in XML as follows:

<&#63;xml version="1.0" encoding="utf-8" &#63;>
<country>
 <name>中国</name>
 <province>
  <name>黑龙江</name>
  <citys>
   <city>哈尔滨</city>
   <city>大庆</city>
  </citys>   
 </province>
 <province>
  <name>广东</name>
  <citys>
   <city>广州</city>
   <city>深圳</city>
   <city>珠海</city>
  </citys>   
 </province>
 <province>
  <name>台湾</name>
  <citys>
    <city>台北</city>
    <city>高雄</city>
  </citys> 
 </province>
 <province>
  <name>新疆</name>
  <citys>
   <city>乌鲁木齐</city>
  </citys>
 </province>
</country>
Copy after login

Use JSON to represent the data of some provinces and cities in China as follows:

 var country =
    {
      name: "中国",
      provinces: [
      { name: "黑龙江", citys: { city: ["哈尔滨", "大庆"]} },
      { name: "广东", citys: { city: ["广州", "深圳", "珠海"]} },
      { name: "台湾", citys: { city: ["台北", "高雄"]} },
      { name: "新疆", citys: { city: ["乌鲁木齐"]} }
      ]
    }
Copy after login

In terms of coding readability, XML has obvious advantages. After all, human language is closer to such a description structure. JSON reads more like a data block and is more confusing to read. However, the language that is difficult for us to read is suitable for machine reading, so the value "Heilongjiang" can be read through the JSON index country.provinces[0].name.

In terms of the handwriting difficulty of coding, XML is more comfortable. It is easy to read and of course easy to write. However, the written JSON characters are obviously much less. If you remove the blank tabs and line breaks, JSON is densely packed with useful data, while XML contains many repeated markup characters.

The above is the entire content of this article, I hope you all like it.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!