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 application 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 network download Basics
What is a network application?
In general, most applications on the iPhone need a network to run, that is, network applications.
Network Application Program Structure
If you run a program on the network to provide data or services for an application, the network application is generally called a client, and the service running on the network is called a server.
Common network interface forms
Common Data interfaces of iOS network applications are generally HTTP URLs.
General Interface Development Process
Download data NSURLConnection + URL
Parse data JSON/XML
Create a data model
Create a view and use View display
TableView + custom cell
Asynchronous image download SDWeblmage
Use of 2NSURLConnection
NSString synchronous download data
NSError * error = nil; 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 ");}
NSURLConnection synchronous download
NSURL * url = [NSURL URLWithString: urlString]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSError * error = nil; NSData * data = [NSURLConnection sendSynchronousRequest: request Response: nil error: & error]; if (error = nil) {NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "str = % @", str);} else {NSLog (@ "Download failed ");}
NSURLConnection asynchronous download
@ Property (copy, nonatomic) NSMutableData * data; // function: // enter the URL. After the download is completed, execute the action method (void) requestWithUrl :( NSString *) in the target object *) url target :( id) target action :( SEL) action; @ interface ZJHttpRequest () <NSURLConnectionDataDelegate> {NSURLConnection * _ connection; NSString * _ url; id _ target; SEL _ action ;} @ end @ implementation ZJHttpRequest // function: // enter the url. After the download is completed, execute the action method (void) requestWithUrl :( NSString *) url target (id) in the target object) target action :( SEL) action {_ url = url; _ target = target; _ action = action; // initiate URL request _ data = [[NSMutableData alloc] init]; _ connection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url] delegate: self startImmediately: YES];}-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[_ data appendData: data];}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// download complete, execute the Save method if (_ target & [_ target respondsToSelector: _ action]) {[_ target using mselector: _ action withObject: self];}
3JSON 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. Complete page implementation (including model creation and use of SDWebImage)