JSON vs. xml in detail

Source: Internet
Author: User
Tags arrays data structures eval php server xmlns

As far as readability and scalability are concerned, JSON and XML are comparable.

In terms of coding difficulty, JSON is a bit simpler than XML, and it's best to write XML with the XML editor, which makes it easy to make mistakes in nesting and pairing manual input. As a result, JSON is superior to XML from the maintainability perspective of the code.

As far as content parsing is concerned, JSON can be resolved by using eval () on the client, including ActionScript, C, C #, ColdFusion, Java, JavaScript, Perl, PHP, Python on the server side, Parsing support for languages such as Ruby is simple, and XML is relatively complex, requiring a large amount of code to be parsed either on the server or on the client side.

In terms of data representation and transmission performance, JSON is significantly simpler than XML, is simple in format, and consumes less bandwidth.

As far as security is concerned, since most JavaScript libraries use eval () to parse data, there are security vulnerabilities to execute malicious JSON data, and a dedicated JSON parser can be used to avoid this problem. XML is more secure than it is.

[XML]

XML is the abbreviation for Extensible Markup language.   Extensible Markup Language XML is a simple data storage language that uses a series of simple tags to describe data that can be built in a convenient way, although XML takes up more space than binary data, but XML is extremely simple and easy to master and use. Unlike databases such as access,oracle and SQL Server, the database provides more robust data storage and analysis capabilities, such as data indexing, sorting, lookup, correlation consistency, and so on, and XML is just presenting data. In fact, the biggest difference between XML and other data forms is that he is extremely simple. This is an advantage that looks trivial, but it makes XML unique.


The simplicity of XML makes it easy to read and write data in any application. This makes XML quickly the only common language for data exchange, although different applications support other data interchange formats, but they will soon support XML, which means that programs can be easier to connect with Windows, Mac OS, The combination of Linux and the information generated under other platforms can then easily load XML data into the program and analyze it and output the results in XML format.

Format

The code is as follows Copy Code


<?xml version= "1.0"?>
<bcpformat
Xmlns= "Http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" >
<RECORD>
<field id= "1" xsi:type= "charterm" terminator= "T"
Max_length= "/>"
<field id= "2" xsi:type= "Charterm" terminator= "T"
Max_length= "collation=" "SQL_Latin1_General_CP1_CI_AS"/>
<field id= "3" xsi:type= "Charterm" terminator= "RN"
Max_length= "30"
collation= "SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<column source= "1" name= "Age" xsi:type= "Sqlint"/>
<column source= "2" name= "FirstName" xsi:type= "Sqlvarychar"/>
<column source= "3" name= "LastName" xsi:type= "Sqlvarychar"/>
</ROW>
</BCPFORMAT>


advantages of using XML as a transport format:
1. Uniform format, conforming to standard
2. Easy to interact with other systems, data sharing is more convenient
3. Invoke an existing service that uses XML as a transport.
4. Use XSLT to dynamically transform XML. This is an ideal feature in the Enterprise Service Bus (ESB) scenario.

Disadvantages:

1. xml file Format file is huge, the format is complex, the transmission occupies the bandwidth
2. Both the server side and the client need to spend a lot of code to parse the XML, regardless of the server-side and client code becoming unusually complex and difficult to maintain
3. Client resolution of XML between different browsers is inconsistent, need to write a lot of code
4. Server-side and client-resolution XML cost resources and time

[JSON]

The specification of JSON is very simple, with only one page of hundreds of words to be clear, and Douglas Crockford claims that this specification never need to upgrade, because the provisions of the rules.


A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).
The ordered list of values (an ordered list of values). In most languages, it is understood as an array.
These are common data structures. In fact most modern computer languages support them in some way. This makes it possible for a data format to be exchanged between programming languages that are also based on these constructs.


1 The data is separated by commas (",").

2) The mapping is represented by a colon (":").

3 the Set (array) of parallel data is represented by square brackets ("[]").

4 the Mapped collection (object) is represented by braces ("{}").

The above four rules are all in JSON format.

For example, the following sentence:

"Beijing has an area of 16800 square kilometres and a permanent population of 16 million people." The area of Shanghai is 6400 square kilometers and the resident population is 18 million. "

The code is as follows Copy Code

This is what you write in the JSON format:

[
{"City": "Beijing", "area": 16800, "Population": 1600},
{"City": "Shanghai", "area": 6400, "Population": 1800}
]


JSON has the following forms:

Object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" followed by a ":" (colon); "' Name/value ' pairs" (comma) separated


Instance

The code is as follows Copy Code

var Testjson = {
"Name": "Obama",
"ByName": ["Xiao Ao", "minibus", "Pony"],
"Education": {
"Gradeschool": "The first elementary school in Washington",
"Middleschool": ["first Middle School in Washington", "first high School in Washington"],
"University": {
"Name": "Harvard University",
"Specialty": ["Software Engineering", "accounting"]
}
}
}


Advantages:
1. The data format is simple, easy to read and write, the format is compressed, occupy a small bandwidth, browser resolution FAST
2. Easy to parse this language, client JavaScript can simply read JSON data via eval ()
3. Structure-friendly, support multiple languages, including ActionScript, C, C #, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other language server-side language, easy server-side resolution
4. In the PHP world, there are already php-json and json-php that are easy to invoke after the PHP serialization process. PHP Server-side objects, arrays, etc. can be directly raw JSON format, easy access to the client.
5. Because the JSON format can be used directly for server-side code, it greatly simplifies the amount of code development for both the server and the client, but the completed tasks are constant and easy to maintain
6. Fairly stable. The additional content of JSON becomes a superset.


Disadvantages:

1. No XML format so popularized by the popular and widely used, without the generality of XML
2. The JSON format is currently promoted in the Web service and is at an early stage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.