Detailed introduction to the differences and usage of json data format and xml data format

黄舟
Release: 2017-03-18 17:19:51
Original
1807 people have browsed it

JSON(JavaScript Object Notation) is a lightweight data exchange format. Easy for humans to read and write, and easy for machines to parse and generate. It is based on a subset of JavaScript (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.

Comparison between JSON and XML
◆Readability
The readability of JSON and XML is comparable, with simple syntax on one side and standardized tags on the other. Form, it's hard to tell the winner.
◆Extensibility
XML is naturally very scalable, and JSON certainly has it. There is nothing that XML can extend that JSON cannot. However, JSON is at home in Javascript and can store Javascript composite objects, which has incomparable advantages over xml.
◆Coding difficulty
XML has a wealth of coding tools, such as Dom4j, JDom, etc., and JSON also provides tools. Without tools, I believe that skilled developers can quickly write the desired XML document and JSONString. However, the XML document requires many more structural characters.
◆Decoding Difficulty
There are two ways to parse XML:
One is to parse through the document model , that is, to produce a set of tags through the parent tag index .
For example: xmlData.getElementsByTagName_r("tagName"), but this must be used when the document structure is known in advance and cannot be universally encapsulated.
Another method is to traverse nodes (document and childNodes). This can be achieved through recursion, but the parsed data is still in different forms and often cannot meet the pre-requirements.
Any such scalable structured data must be very difficult to parse.
The same is true for JSON. If you know the JSON structure in advance, using JSON for data transfer is simply wonderful. You can write code that is very practical, beautiful and readable. If you are a pure front-end developer, you will definitely like JSON very much. But if you are an application developer, you don't like it so much. After all, xml is the real structured markup language, used for data transfer.
And if you don’t know the structure of JSON and parse JSON, it would be a nightmare. Not only is it time-consuming and labor-intensive, the code will also become redundant and protracted, and the results obtained will be unsatisfactory. But this does not affect many front-end developers choosing JSON. Because toJSONString() in json.js can see the string structure of JSON. Of course not using this string, which is still a nightmare. After people who often use JSON see this string, they will have a clear understanding of the structure of JSON, and it will be easier to operate JSON.
The above is the parsing of xml and JSON only for data transmission in Javascript. In the field of Javascript, JSON is the home field after all, and its advantages are of course far superior to xml. If Javascript composite objects are stored in JSON and their structure is not known, I believe many programmers will also cry when parsing JSON.
◆Example comparison
Both XML and JSON use structured methods to mark data. Let’s make a simple comparison below.
Use XML to represent data of some provinces and cities in China as follows:

<?xml version="1.0" encoding="utf-8"?>
    <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>

    用JSON表示如下:
    {
    name:"中国",
    province:[
    {
    name:"黑龙江",
    citys:{
    city:["哈尔滨","大庆"]
    }
    },
    {
    name:"广东",
    citys:{
    city:["广州","深圳","珠海"]
    }
    },
    {
    name:"台湾",
    citys:{
    city:["台北","高雄"]
    }
    },
    {
    name:"新疆",
    citys:{
    city:["乌鲁木齐"]
    }
    }
    ]
    }
Copy after login

XML has obvious advantages in terms of readability of encoding. 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 exactly suitable for machine reading, so the value "Heilongjiang" can be read through the json index .province[0].name. Regarding the handwriting difficulty of coding, xml is still more comfortable. It is easier to read and of course easier 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 detailed content of Detailed introduction to the differences and usage of json data format and xml data format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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