Comparison of JSON and XML on iOS, iosjson

Source: Internet
Author: User
Tags php server

Comparison of JSON and XML on iOS, iosjson

Data exchange with web services usually supports two major data formats (JavaScript Object Notation JSON and extensible markup language XML), both of which are highly readable, next, we will make a brief summary and analysis on this. Please give us some advice on anything inappropriate.

I. Introduction XML

XML (Extensible Markup Language), as a markup language used to mark electronic folders to make them structured, is designed to transmit data;

In a computer, a tag refers to an information symbol that a computer can understand. By marking it, computers can process various types of information data;

It is a content-based cross-platform technology in the Internet environment. It is suitable for World Wide Web transmission and provides a unified method for describing and exchanging structured data independent of applications or vendors, it can be used to mark and define data types. It is a source language that allows users to define their own markup language and is very suitable for web transmission operations;

XML in iOS, the DOM parser and the SAX Parser can be used to parse it. SAX is a streaming parser that traverses the entire XML document one by one and returns parsed data through the callback function. most SAX parsers use a URL as a parameter. After parsing the target data, the data is returned.

For example:

The NSXMLParser class has a method (initWithContentsOfURL :) you only need to use the URL to initialize a parser. NSXMLParser will process the remaining operations;

The system calls back the parsed data by calling the delegate method defined in NSXMLParserDelegate. However, since the parser uses the delegate to return data, each object to be processed must have a NSObject subclass that implements NSXMLParserDelegate. compared with the DOM parser, the Code is not concise. in addition, the DOM parser first needs to load the entire XML file into the memory to start parsing. The advantage of the DOM parser is that it can use XPath to query and access random data, you do not need to use delegation like the SAX model.

Note: If a class is used for implementationNSXMLParserDelegate: the code becomes uncontrollable if the XML format is changed.

JSON

JSON (JavaScript Object Notation), as a lightweight data exchange format, is more commonly used than XML and uses a language-independent text format, with good readability and fast writing, data can be exchanged between different platforms, and JSON can represent a more complex structure than "name/value pairs, for example, arrays and complex object structures. Although Apple officially provides a JSON Processing framework, it is a private API in iOS 4 and cannot be used by developers, in iOS 5, NSJSONSerialization is introduced to parse JSON.

Basic Structure

1. A set of "name/value pairs". In different languages, it is understood as an object, record, structure, Dictionary, hash table, a key list, or an associated array;

2. ordered list of values. In most languages, it is understood as an array.

Ii. Comparison XMLAdvantages:

Uniform format, compliant with standards;

It is easy to remotely interact with other systems, making data transmission and sharing easier.

Disadvantages:

Large files, complex file formats, and bandwidth occupied by transmission;

Both the server and client need to spend a lot of code to parse the XML, resulting in abnormal and complicated and bloated server and client code and difficult to maintain;

The XML parsing methods for different browsers on the client are different, so you need to repeat a lot of code;

Parsing XML on the server and client consumes a lot of resources and time.

JSON advantages:

The data format is relatively simple and easy to read/write operations. The format is compressed and the bandwidth occupied is small;

It is easy to parse, and the client JavaScript can simply read JSON data through eval;

Supports multiple languages, including ActionScript, C, C #, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby, and other server-side languages, making server-side parsing easier;
In PHP has a PHP-JSON and JSON-PHP conversion method, to facilitate PHP serialized program directly called, PHP server-side object number, groups can directly generate JSON format, easy to access the client extraction;

Because the JSON format can be used directly for server code, it simplifies the code magnitude on the server and client while ensuring the task is completed, and is easier to maintain.

Disadvantages:

It is not as high as XML universality. It is not easy to make JavaScript code that has the correct syntax on the server side. This situation mainly occurs in a large system, the server and client have different developers, so they must negotiate the data format of the object, which can easily cause errors;

JSON is slightly less descriptive than XML.

Iii. Summary

Readability: the two are basically the same. We recommend that you use the syntax and standard label format. XML occupies the upper peak;

Scalability: Both of them have good scalability. XML has inherent scalability advantages, but JSON also has;

Encoding difficulty: Both of them have tools, but JSON encoding is obviously easier than XML. JSON code can be written even without tools, which is more advantageous;

Difficulty in decoding: JSON decoding has no pressure, while XML requires nodes and parent nodes;

Data volume: JSON has a higher transmission speed than XML;

Interaction: The Interaction Between JSON and JavaScript is more convenient and easy to parse, with better data interaction advantages;

Data Description: XML data description is better than JSON;

Data transmission: There is no doubt that JSON is far greater than XML.

Processing JSON on iOS platform is an order of magnitude easier than processing XML. If the server supports both XML and JSON formats, it is recommended to select JSON;

If the server has not been developed, we recommend that you start with JSON.

Related Article

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.