Home > Article > Backend Development > A detailed introduction to the advantages and disadvantages of JSON and XML
JSON(Javascript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It is based on Javascript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, Javascript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.
The concept of JSON is very simple, that is, the server directly generates Javascript statements, and the client directly uses the eval method to obtain the object after obtaining it, thus eliminating the cost of parsing XML.
If you want to load information from the background, write it in 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>
And write it in JSON:
[ { 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" } ]
It’s not only simple in expression, but the most important thing is that it can be discarded and make people confused. The DOM is parsed. Because as long as it meets the Javascript declaration specifications, JavaScript will automatically parse it for you. The basic method of using JSON in Ajax is to load the string of the background declaration Javascript object in the frontend, use the eval method to convert it into an actual object, and finally update it through DHTML Page information.
JSON not only reduces the performance problems and compatibility issues caused by parsing XML, but is also very easy to use for Javascript, which can conveniently traverse arrays and access objects Attribute is used to obtain data. It is also very readable and basically has the properties of structured data. I have to say it is a good method, and in fact Google Maps does not use XML to transfer data, but uses the JSON solution.
Another advantage of JSON is "cross-domain". For example, if you use
in the web page of www.Web.cn, one A lightweight data exchange format that is easy to read and easy to write quickly. The mainstream technology in the industry provides a complete solution (somewhat similar to regular expression, which is supported by most languages today), allowing data exchange between different platforms. JSON adopts a highly compatible text format and also has behavior similar to the C language system.
Extensible Markup Language (XML), a markup language used to mark electronic documents to make them structural, can be used to mark Data, definitionData type is a source language that allows users to define their own markup language. 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.
[ XML ]
Advantages of using XML as the transmission format:
Uniform format, compliant with standards
Easy to remotely interact with other systems, data sharing is more convenient
Disadvantages:
XML file format has huge files, complex format, and transmission Bandwidth occupied
Both the server and the client need to spend a lot of code to parse XML, regardless of whether the server or client code becomes extremely complex and difficult to maintain
The way of parsing XML between different browsers on the client side is inconsistent, and a lot of code needs to be written repeatedly
It takes resources and time to parse XML on the server side and client side
[ JSON ]
So besides XML format, are there any other formats? There is a lightweight data exchange format called JSON (JavaScript Object Notation) that can replace XML.
Advantages:
The data format is relatively simple, easy to read and write, the formats are compressed, and takes up little bandwidth
Easy to parse this Language, client-side JavaScript can simply read JSON data through eval_r()
supports multiple languages, including ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other server-side languages facilitate server-side parsing
In the PHP world, PHP-JSON and JSON-PHP have already appeared , which facilitates direct calling of PHP serialized programs. PHP server-side objects, arrays, etc. can directly generate JSON format, which facilitates client access and extraction.
Because the JSON format can be used directly by server-side code, it greatly simplifies the code development of server-side and client-side, but the completed tasks remain the same and is easy to maintain
Disadvantages:
The XML format is not as popular and widely used, and it is not as versatile as XML
The JSON format is currently Promotion in Web Service is still in its infancy
Comparison of the advantages and disadvantages of JSON and XML
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 recommended syntax, and the other side is the standardized tag form. It is difficult to distinguish the winner.
In terms of scalability, XML is naturally very scalable, and JSON certainly has it. There is nothing that XML can expand that JSON cannot.
In terms of coding difficulty, XML has a wealth of coding tools, such as Dom4j, JDom, etc. JSON also has tools provided by json.org, but JSON coding is obviously much easier than XML, even if JSON code can be written without the help of tools, but it is not easy to write XML well.
In terms of decoding difficulty, the parsing of XML has to consider the child nodes and parent nodes, which makes people dizzy, while the parsing difficulty of JSON is almost 0. XML loses really nothing at this point.
In terms of 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).
JSON and XML also have rich parsing methods.
Compared with XML, JSON has a small data size.
The interaction between JSON and JavaScript is more convenient.
JSON is less descriptive of data than XML.
JSON is much faster than XML.
1. Comparison of data exchange formats about XML and JSON:
XML: extensible markup language, a language similar to HTML. It has no predefined tags and 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. For specific information, you can ask 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 for humans to read and write. It is also easy for machines to parse and generate. It is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.
2. Comparison of data exchange formats about lightweight and heavyweight:
Lightweight and heavyweight are relative terms, so where is the heavyweight of XML compared to JSON? I think it should be reflected In terms of parsing, XML is currently designed with two parsing methods: DOM and SAX;
DOM treats a data exchange format XML as a DOM object, and the entire XML file needs to be read into the memory. This At one point, the principles of JSON and XML are the same, but XML must consider parent nodes and child nodes. At this point, JSON is much less difficult to parse, because JSON is built on two structures: key/value, key A collection of value pairs; an ordered collection of values, which can be understood as an array;
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 that: JSON only provides an overall parsing solution, and this method can only achieve good results when parsing less data; while XML provides a large-scale A step-by-step data parsing scheme, which is very suitable for processing large amounts of data.
3. Comparison of data exchange formats regarding the difficulty of data format encoding and parsing:
In terms of encoding, although XML and JSON have their own encoding tools, JSON encoding is simpler than XML, even without the help of tools , you can also write JSON code, but it is a bit difficult to write good XML code; like XML, JSON is also text-based, and they both use Unicode encoding, and it is as readable as the data exchange format XML.
Subjectively, 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 worry about parsing XML. 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 that is extremely low-cost. development efficiency.
In fact, for most web applications, they do not need complex XML to transmit data at all, and the extensibility claimed by XML rarely has an advantage here; many Ajax applications even directly return HTML fragments to build dynamic webs page. 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.
The above is the detailed content of A detailed introduction to the advantages and disadvantages of JSON and XML. For more information, please follow other related articles on the PHP Chinese website!