Network Programming---JSON and XML

Source: Internet
Author: User

1.json1. What is JSON

JSON is a lightweight data format that is typically used for data interaction
Data returned by the server to the client, usually in JSON format or XML format (except for file downloads)

The format of JSON is much like the dictionary and array in OC
{"Name": "Jack", "Age": 10}
{"Names": ["Jack", "Rose", "Jim"]}

Note points in the standard JSON format: key must be double-quoted

To dig out specific data from JSON, you have to parse the JSON
JSON conversion to OC data type

2.JSON–OC Conversion Table


3.JSON Parsing Solution

In iOS, there are 4 common parsing scenarios for JSON
Third-party frameworks: Jsonkit, Sbjson, Touchjson (performance from left to right, worse)
Apple native (comes with): Nsjsonserialization (Best performance)

Common methods of Nsjsonserialization:

OC object, JSON data

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;

OC Object--JSON data

+ (NSData *)dataWithJSONObject:(id)objoptions:(NSJSONWritingOptions)opterror:(NSError **)error;

Parse the JSON from the server:


2.xml1. What is XML

Full name is extensible Markup Language, translated "Extensible Markup Language"
Like JSON, it's also a common format for interacting data.
Generally also called XML document (XML documents)

XML Example

<videos>    name="小黄人 第01部"length="30" />    name="小黄人 第02部"length="19" />    name="小黄人 第03部"length="33" /></videos>
2.XML syntax

A common XML document typically consists of the following sections:
Document Declaration
Elements (Element)
Properties (Attribute)

At the very front of the XML document, you must write a document declaration that declares the type of the XML document:

//最简单的声明<?xml version="1.0" ?>//用encoding属性说明文档的字符编码<?xml version="1.0" encoding="UTF-8" ?>
1. Elements:

An element includes a start tag and an end tag
Elements that have content: <video>小黄人</video>
Elements with no content: <video></video>
Shorthand for elements without content:<video/>

An element can nest several child elements (no cross-nesting)

<videos>    <video>        <name>小黄人 第01部</name>          <length>30</length>    </video></videos>

Canonical XML documents have a maximum of 1 root elements, and other elements are descendants of the root element

all spaces and line breaks in XML are treated as concrete content
The contents of the following two elements are not the same
1th One

<video>小黄人</video>

2nd One

<video>    小黄人</video>
2. Properties

An element can have multiple properties:

name="小黄人 第01部"length="30" />

The video element has a name and length two properties
Attribute values must be enclosed in double quotation marks "" or single quotation marks

In fact, the information represented by a property can also be represented by a child element, such as:

<video>    <name>小黄人 第01部</name>        <length>30</length></video>
3.XML parsing

1. To extract useful information from XML, you must learn to parse the XML
Extract the contents of the name element:

<name>小黄人 第01部</name>

Extract the value of the name and length property in the video element:

name="小黄人 第01部"length="30" />

There are 2 ways to parse 2.XML:
DOM: Loading an entire XML document into memory at once, which is more suitable for parsing small files
SAX: Starting from the root element, in order to parse an element down, it is more suitable for parsing large files

in iOS, there are many ways to parse XML
Apple native
Nsxmlparser: Sax method Parsing, simple to use

Third-party frameworks
libxml2: pure C language, which is included by default in the iOS SDK while supporting DOM and sax parsing
gdataxml: Dom mode parsing, developed by Google, based on LIBXML2

Suggestions for choosing XML parsing methods:
Large files: Nsxmlparser, LIBXML2
Small files: Gdataxml, Nsxmlparser, LIBXML2

3.NSXMLParser

Steps to use:

// 传入XML数据,创建解析器NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];// 设置代理,监听解析过程parser.delegateself;// 开始解析[parser parse];

Nsxmlparser take is the sax parsing, characterized by event-driven, the following situation will notify the agent
When scanning to the beginning and end of documents (document)
When scanning to the start and end of elements (element)

Proxy method:

Called when scanning to the beginning of a document (parsing is started)-(void)Parserdidstartdocument:(nsxmlparser *)parser//called when scanning to the end of the document (parsing completed)-(void)Parserdidenddocument:(nsxmlparser *)parser//is called when scanning to the beginning of an element (Attributedict holds the attributes of the element)-(void)Parser(nsxmlparser *)Parser Didstartelement:(nsstring *)ElementName NamespaceURI:(nsstring *)NamespaceURI QualifiedName:(nsstring *)QName attributes:(nsdictionary *)attributedict//called when scanning to the end of an element-(void)Parser(nsxmlparser *)Parser Didendelement:(nsstring *)ElementName NamespaceURI:(nsstring *)NamespaceURI QualifiedName:(nsstring *)Qname
3.GDataXML

1.GDataXML Configuration

Gdataxml based on the LIBXML2 library, the following configuration is done:
1. Import the LIBXML2 library:


2. Set the LIBXML2 header file search path (in order to find all header files for the LIBXML2 library)
Add/USR/INCLUDE/LIBXML2 to head Search path

3. Set link parameters (Auto link libxml2 library)
Add-LXML2 to other Linker flags

4. Because the Gdataxml is non-arc, you have to set the compilation parameters


Classes commonly used in 5.GDataXML:
gdataxmldocument: representing the entire XML document

gdataxmlelement:
Represents each element in a document
Use the attributeforname: method to get the property value

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Network Programming---JSON and XML

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.