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 URLConnection to download data from the network, parse the downloaded JSON data format, and display a part of the downloaded data and images.

Knowledge points involved:

1. asynchronous download and encapsulation of URLConnection

2. JSON format and JSON Format Parsing

3. data display and asynchronous image display using SDWebImage

 

Content 1. Introduction to basic network download knowledge what is a network application?

Applications that need to connect to the network, such as QQ and note;

Computers, cameras, calendars, and others that do not need to connect to the network are called local applications.

Network Application Program Structure

Clients and servers are required

The server serves the client, such as providing resources to the client and saving client data.

A Client, or a Client, is a program that provides local services to a Client in response to a server.

Common network interface forms

Half of common data interfaces of IOS network applications are HTTP URLs;

Other network data interfaces: http, ftp, and https

Common Data Formats

There are two common data formats in IOS development: JSON format and XML format. JSON format accounts for 75% of the total number of data formats, and XML 20% of the total number, the remaining accounts for 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 cell

2. Use URLConnection

NSString synchronous download

NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; NSLog (@" % @ ", urlString); // 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 will return a false UI and cannot be used. // 2. use asynchronous download (NSURLConnection asynchronous download)

NSURLConnection synchronous download

NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // send a synchronous URL request // NSURLRequest URL request NSURL * url = [NSURL URLWithString: urlString]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSError * error = nil; 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 ");}

 

NSURLConnection asynchronous download

-(Void) testtestNSURLConnectionAsyncDownloadData {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 the download and return immediately. During the download process, run _ connection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: urlString] delegate: self startImmediately: YES]; NSLog (@ "initWithRequest execution completed");} // proxy method: Response execution received from the server-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "received Server response");} // proxy method: when receiving data, execute // Note: when the data is large, it may be executed multiple times-(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 ); // The simplest real name // parse JSON // function: Convert JSON data to NSDictionary or NSArray NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: _ data options: NSJSONReadingMutableContainers error: nil]; NSArray * appList = dic [@ "applications"]; for (NSDictionary * appDic in appList) {NSLog (@ "name = % @", appDic [@ "name"]) ;}// proxy method: data download failed-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "error = % @", error );}

 

3. JSON format description and formatting Tool
{"List": [{"IsXuanZuo": 0, "Name": "Wang Feng 2014" peak storm coming "Super Tour concert Chongqing Station", "PriceStr": "180,280,380,480,680,980,128 0 ", "ProjectID": 66611, "ShowTime": "2014.11.01", "SiteStatus": 8, "Summary": "[advance payment notice] enable advance payment at on April 9, May 12, 2014! "," VenId ": 258," VenName ":" Chongqing Olympic Sports Center "," cityname ":" Chongqing "," priceName ":" 180-1280 "}, {"IsXuanZuo": 0, "Name": "mo Wenwei's 20 th Anniversary Tour of Zhengzhou Station", "PriceStr": "480,680,880,128 2014, 66313", "ProjectID, "ShowTime": "2014.07.26", "SiteStatus": 8, "Summary": "[client exclusive] 480 yuan a small number of scarce tickets exclusively sold on the client! "," VenId ": 942," VenName ":" Zhengzhou International Convention and Exhibition Center "," cityname ":" Zhengzhou City "," priceName ":"-yuan "}, {"IsXuanZuo": 0, "Name": "Wang Lihong 2014MUSIC-MANII" Fire all open "world tour concert-Beijing Station", "PriceStr": "280,380,480,580,880,128", "ProjectID ": 66052, "ShowTime": "2014.06.14", "SiteStatus": 8, "Summary": "tanks are under fire, creating a music battle! New Interpretation of classic works, experience extraordinary music charm! "," VenId ": 392," VenName ":" National Stadium "," cityname ":" Beijing "," priceName ":" 280-1680 RMB "}]}

[] 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

JSON format formatting Tool

Jason

Json Editor

Online: http://www.kjson.com/

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

:

 

My code: Download

 

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.