IOS network data download and JSON parsing, and ios data json Parsing

Source: Internet
Author: User

IOS network data download and JSON parsing, and ios data json Parsing
Introduction to iOS network data download and JSON Parsing

In this article, I will introduce how to use NSURLConnection to download data from the network, parse the downloaded JSON data format, and display the asynchronous download of data and images.

Knowledge points involved:

1. NSURLConnection asynchronous download and Encapsulation

2. JSON format and JSON Format Parsing

3. data display and asynchronous image display using SDWebImage

Content 1. Introduction to basic network download knowledge

(1) What is a network application?

Generally, iPhone computers and cameras can run without downloading data from the network. Therefore, this type of application is a local application, but most applications on the iPhone can run through the network, for example, QQ, Xiami music, so in iOS development, you need to know how to download data from the network

 

(2) program structure of network applications

Different from local applications, network application data is downloaded from the network. Therefore, you need to run a program on the network to provide data or services for the application, therefore, this network application is generally called a client, and the service running on the network is called a server.

 

(3) Common network interface forms

IOS network application common data interface is generally in the form of http url address, for example, love free application home page data address for http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id =

In a project, some open-source libraries are generally used to download data through this URL. For example, AFNetworking

 

(4) Common Data Formats

There are two common data formats in iOS development: JSON format and XML format. JSON format is usually used.

 

(5) general interface development process

Develop an interface in iOS that requires the interface, interface material resources, and network interfaces

The development process is generally as follows:

1. download data

2. parse JSON or XML data and create a data model

3. Use Controls to display data and customize views when necessary, such as custom cells.

2. Use NSURLConnection

NSURLConnection asynchronous download

# Pragma mark-NSURLConnection asynchronous download-(void) testNSURLConnectionAsyncDownloadData {NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // initialize _ data = [[NSMutableData alloc] init]; // initiate an asynchronous URL Connection Request // asynchronous: after the method is executed, start downloading and return immediately // The download process is executed in the background (multi-thread) _ connection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: urlString] delegate: self startImmediately: YES];} // proxy method: receive Server Response execution-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "received Server response execution");} // proxy method: executed when data is received // Note: when the data is large, multiple executions-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[_ data appendData: data];} // proxy method: data download completed-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// NSString * str = [[NSString alloc] initWithData: _ data encoding: NSUTF8StringEncoding]; // NSLog (@ "str = % @", str); // simplest: display the names of all applications // parse JSON // function: JSON data is converted to NSArray or NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: _ data options: NSJSONReadingMutableContainers error: nil]; NSLog (@ "dic =%@", dic ); NSArray * appList = dic [@ "applications"]; // = NSArray * appList = [dic valueForKey: @ "applications"]; for (NSDictionary * appDic in appList) {NSLog (@ "name = % @", appDic [@ "name"]) ;}//-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "error = % @", error );}

 

NSURLConnection synchronous download

# Pragma mark-NSURLConnection synchronous download-(void) testNSURLConnectionSyncDownloadData {NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // send a synchronous URL request // NSURLRequest URL request object NSError * error = nil; NSURL * url = [NSURL URLWithString: urlString]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: & error]; if (error = nil) {NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "str = % @", str );} else {NSLog (@ "Download failed ");}}

 

NSString synchronous download

# Pragma mark-NSString synchronous download-(void) testNSStringDownloadData {// use the URL address in HTTP // http: // use the Protocol (ftp ://) // iappfree.candou.com host address (Domain Name and IP address) //: 8080 host port // free/applications/limited web page program file path //? Currency = RMB & page = 1 & category_id = program parameters (parameters are separated with &) NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // download data // generate the NSURL object NSError * error = nil through the address; NSURL * url = [NSURL URLWithString: urlString]; NSString * content = [[NSString alloc] initWithContentsOfURL: url encoding: NSUTF8StringEncoding error: & error]; if (error = nil) {NSLog (@ "content = % @", content);} else {NSLog (@ "Download failed");} // how to use it in the project // 1. download in synchronous mode. After initWithContentsOfURL is downloaded, the system returns a result. // The interface is suspended and cannot be used. // 2. use asynchronous download (NSURLConnection asynchronous download )}

 

3. JSON format description and formatting Tool

JSON format

{

"Count": 20,

"Data ":[

"Zhangsan ",

"Lisi ",

"Wangwu"

]

}

[] Indicates an array, corresponding to NSArray

, Indicating the parallel data

{} Indicates the dictionary, corresponding to NSDictionary

: Indicates a key-value pair.

"Xxx" indicates a string, corresponding to NSString

20 corresponding NSNumber

 

4. Implementation of a complete page (including the creation of a model and the use of SDWebImage)

:

 

 

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.