IOS request data parsing XML and JSON

Source: Internet
Author: User

I haven't written an article for a long time. Let's record the previous content.

This section mainly involves data parsing. Let's talk about data parsing.
Currently, data parsing generally parses two types of data: xml and JSON.
Let's start with xml parsing.
An NSXMLParser class and a protocol NSXMLParserDelegate are required for xml parsing.
First, NSXMLParser is an xml parser.

To parse data, first create an xml Parser

NSXMLParser * XMLParser = [[NSXMLParser alloc] initWithData: data];

Data is the data to be parsed.


Then perform the parsing on the line [XMLParser parse];
Several Methods in the following protocol will be called back when parsing is executed:

# Pragma mark NSXMLParserdelegate ---
// Start parsing the document
-(Void) parserDidStartDocument :( NSXMLParser *) parser
{
// Called when the document starts parsing. Some things can be initialized here
}

// The parsing document ends
-(Void) parserDidEndDocument :( NSXMLParser *) parser
{
// Called when the document Parsing is complete. Here you can write the operation after the parsing is complete.
}

// Tag resolution starts
-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict
{
// Tag start parsing, where elementName is the name of the start tag
}

// Tag resolution ends
-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName
{
// Tag start parsing, where elementName is the name of the end tag,
// Here you can make some logical judgments to see if the items in the tag are needed and then perform operations
}

// Resolved tag Value
-(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string
{
// String indicates the value of the currently resolved tag,
}


In fact, there is another method for xml parsing, namely dom parsing, which regards XML as a tree structure and first finds the root node,
Then, nested loops are used to traverse the child nodes of the root node, traverse the child nodes of the child node, and traverse the child nodes until the leaf node. Of course, the leaf node
The last layer is where we need data.
This method is not easy to use. It is too troublesome and contains too many nested loops. Therefore, it is not recommended to use
The libxml2.dylib class library needs to be imported, and the path/usr/include/libxml2 must be modified.
But here I will give this third-party class.
Http://pan.baidu.com/s/1sjtXAUh


The next step is our JSON parsing.
I feel that the parsing process of JSON Parsing is not important. What is important is the processing of the result after JSON parsing.
JSON is a dictionary after resolution, but the dictionary may contain dictionaries and arrays, And the array can also contain dictionaries.
This is the result of JSON parsing of the data returned by Sina Weibo's request to Weibo.
The following describes the JSON parsing process.
In fact, just one sentence

Data is the parsing data.
// The built-in parsing class NSJSONSerialization of IOS5 parses the data from the data and puts it into the dictionary.
NSDictionary * weatherDic = [NSJSONSerialization JSONObjectWithData: Data options: NSJSONReadingMutableLeaves error: & error];

// TouchJSON
// Store the parsed content in the dictionary
NSDictionary * pRootDic = [[CJSONDeserializer deserializer] deserialize: data error: & error];
// The parsing object here is of the NSData type

// SBJSON
// Obtain a parser object
SBJsonParser * parser = [[SBJsonParser alloc] init] autorelser];
// Retrieve the dictionary returned after json String Parsing
NSDictionary * rootDic = [parser objectWithString: String error: & error];
// Note that the parsed data must be decoded to the NSString type before parsing.

// JSONKit
// Obtain the resolved Dictionary (the returned value is of the dictionary type after resolution is completed)
NSDictionary * pWeatherDic = [jsonStr objectFromJSONStringWithParseOptions: JKParseOptionLooseUnicode]; // unified encoding standard
// Note that jsonStr decodes the data to the NSString type first, and then jsonStr directly uses the parsing method.


// Here comes a method for converting data to the NSString type
NSString * string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

Links to third-party classes of TouchJSON, SBJSON, and JSONKit are attached below.
Http://pan.baidu.com/s/1bnpGwxD



Now, I am here to share my recent knowledge with you. If you have any mistakes, please let us know-LC




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.