IOS network data download and Json data parsing, and iosjson data parsing
Introduction to iOS network data download and Json data parsing
In this article, I will introduce how to use NSURLConnection to download data from the network, how to parse the downloaded JSON data, and how to display the asynchronous download and display of data and images.
Knowledge points involved:
1. NSURLConnection asynchronous download 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 half of the form of http url address, such as the love limit 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 cell
2. Use NSURLConnection
NSURLConnection asynchronous download and synchronous download, NSString synchronous 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]; // initiates 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]; NSLog (@ "initWithRequest execution completed");} // proxy method: receive Server Response execution-(void) connection :( NSURLConnection *) connection didReceive Response :( NSURLResponse *) response {NSLog (@ "receive Server Response execution");} // proxy method: Run when data is received // Note: when data is large, multiple executions-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[_ data appendData: data];} // proxy method: -(void) connectionDidFinishLoading :( NSURLConnection *) connection {// NSString * str = [[NSString alloc] initWithData: _ data encoding: NSUTF8StringEncoding]; // NSLog (@ "str = % @", str); // The simplest: Display All application names // parse JSON // function: Convert JSON data to NSArray or NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _ data options: NSJSONReadingMutableContainers error: nil]; // NSLog (@ "dict = % @", dict); // NSArray * appList = dict [@ "applications"]; NSArray * appList = [dict valueForKey: @ "applications"]; for (NSDictionary * appDict in appList) {NSLog (@ "name = % @", appDict [@ "name"]);} // NSArray * a = @ [@ "t Est ", @" vec "]; // NSDictionary * d =@{ @" k1 ": @" v1 ", @" k2 ": @" v2 "}; // NSLog (@ "v = % @", d [@ "k1"]);}-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "error = % @", error) ;}# 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 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]; N SLog (@ "str = % @", str);} else {NSLog (@ "Download failed") ;}# pragma mark-NSString synchronous download-(void) testNSStringDownloadData {// use the URL address in HTTP // http: // address use 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 // JavaScript Object Notation/* {"count": 20, "data": ["zhangsan", "lisi ", "wangwu"]} * // [] indicates the array, corresponding to NSArray //, indicating the parallel data // {} indicates the dictionary, corresponding to NSDictionary //: key-Value Pair // "ta" indicates the string, corresponding NSString // 20 corresponding NSNumber // JSON format formatting tool // Jason // Json Editor // online: http://www.kjson.com/
4. Implementation of a complete page (including model creation and use of SDWebImage)
: