Home Backend Development XML/RSS Tutorial Detailed introduction to the differences and usage of json data format and xml data format

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

Mar 18, 2017 pm 05:19 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese Mar 05, 2024 pm 02:48 PM

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles