Currently, in the Web development field, the main data exchange formats are XML and JSON, so I believe that every web developer is familiar with XML. In contrast, JSON may be unfamiliar with some new developers, or you may have heard of it before, but you may not know much about the differences between XML and JSON. In Ajax development, the choice of XML or JSON remains controversial. individuals prefer JSON. Although JSON is in its infancy, I believe JSON will certainly develop well.
Let's take a look at the advantages and disadvantages of XML and JSON:
[XML]
Advantages of using XML as the transmission format:
1. unified format, compliant with standards
2. Easy remote interaction with other systems and convenient data sharing
Disadvantages:
1. xml file format is large, complex, and occupies bandwidth 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 method for parsing XML between different browsers on the client is inconsistent. 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. It is easy to parse this language. The client javascript can simply read JSON data through eval ().
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 a PHP-JSON and a JSON-PHP that facilitates PHP Serialized programs to call directly. PHP server objects and arrays can directly generate JSON format to facilitate client access extraction.
5. JSON format can be used directly for server-side code, which greatly simplifies the development of server-side and client-side code, but the completed tasks remain unchanged and easy to maintain.
Disadvantages:
1. It is widely used and widely used without the XML format. It is not as universal as XML.
2. The JSON format is still in the initial stage in Web service promotion.
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 several groups;
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. Like XML, JSON is also text-based and both use Unicode encoding, and it 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.