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 and display of data and images.
Knowledge points involved include:
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 IOS development needs 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
Half of common data interfaces of IOS network applications are http url addresses. For example, the data address of the homepage of the elasticsearch application is:
Http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id =
In projects, some open-source libraries are generally used to download data through such websites. 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
I interfaces are developed in IOS. interfaces, material resources and network interfaces are required.
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, such as Cell, when necessary.
2. Use NSURLConnection
NSString synchronous download data...-(void) testNSStringDownloadData
{NSString * URLstr = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // 1. http is the transmission protocol name // 2.iappfree.candou.com is the domain name NSURL * url = [NSURL URLWithString: URLstr]; NSError * error = nil; NSString * content = [[NSString alloc] initWithContentsOfURL: url encoding: NSUTF8StringEncoding error: & error]; if (error = nil) {NSLog (@ "content = % @", content );} else {NSLog (@ "Download failed");} // The above is a synchronous download and cannot be used; //}-(void) NSURLConnectionSynDownloadData {NSString * URLstr = @ "htt P: // iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; NSError * error = nil; NSURL * url = [NSURL URLWithString: URLstr]; 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);} else {NSLog (@ "Download failed ");}}
3. JSON format description and formatting tool 4. A complete page implementation (including model creation and SDWebImage usage)