Home Web Front-end JS Tutorial Detailed explanation of the use of JSON and XML

Detailed explanation of the use of JSON and XML

Apr 25, 2018 pm 05:18 PM
javascript json use

This time I will bring you a detailed explanation of the use of JSON and XML. What are the precautions when using JSON and XML? Here are practical cases, let’s take a look.

1. Definition introduction

1.1 XML definition

Extensible Markup Language (XML), used A markup language that marks electronic files to make them structural, which can be used to mark data and define data types. It is a source language that allows users to define their own markup 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 transmission. 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 well readable and convenient. Features for writing quickly. Data exchange between different platforms is possible. JSON adopts a highly compatible and completely language-independent text format, and also has system behavior similar to C language (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 with other systems remotely and data sharing is more convenient.

Disadvantages of XML
A. The XML file is huge, the file format is complex, and the transmission takes up bandwidth;
B. Both the server and the client need to spend a lot of code to parse XML, causing the server and client to The code on the client side becomes 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. It costs more to parse XML on the server side and client side resources and time.

2.2 Advantages and Disadvantages of JSON

Advantages of JSON:

A. The data format is relatively simple, easy to read and write, and the formats are compressed. Occupies 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 ​​facilitate server-side parsing;
D. In the PHP world, PHP-JSON and JSON-PHP have already appeared, and it is preferred to directly call the program after PHP serialization , PHP server-side objects, arrays, etc. can directly generate JSON format, which facilitates client access and extraction;
E. Because JSON format can be directly used by server-side code, it greatly simplifies the amount of code development on the server side and client side, and completes The task remains unchanged and it is easy to maintain.

Disadvantages of JSON

A. It is not as popular and widely used as the XML format, and it is not as versatile as XML;
B. The JSON format is currently not promoted in Web Service Belongs to the primary stage.

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 it is not easy to write XML well.

Decoding difficulty aspect.

The parsing of XML has to consider the child nodes and parent nodes, which is dizzying, 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, and 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).

Analysis means.

JSON and XML also have rich parsing methods.

In terms of data volume.

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

Data interaction aspect.

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.

In terms of 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 is relative, so where is the heavyweight of XML compared to JSON? It should be reflected in parsing. XML currently has 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. This is 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, a collection of key-value pairs; An ordered collection 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 Methods. 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.
So, 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 It provides a step-by-step analysis 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 encoding.

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; Like XML, JSON is also text-based, and they all use Unicode encoding, and it is as readable as the data exchange format XML.

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 analysis.

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 leads to complex code and extremely low latency. Development efficiency.

In fact, for most Web applications, they do not need complex XML to transmit data at all. The scalability claimed by XML rarely has an advantage here. Many Ajax applications even directly return HTML fragments to build dynamic Web 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.


Example comparison

XML and JSON both use structured methods to mark data. Let’s make a simple comparison below.

Use XML to represent the 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>
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 the readability of encoding, XML has obvious advantages. , after all, human language is closer to such an explanation 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

jsonp json implements AJAX cross-domain request

How can Jsonp solve ajax cross-domain request

The above is the detailed content of Detailed explanation of the use of JSON and XML. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Mar 10, 2024 pm 04:34 PM

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.

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 to use Xiaomi Auto app How to use Xiaomi Auto app Apr 01, 2024 pm 09:19 PM

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

See all articles