IOS network data download and JSON parsing, ios network 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:
1. NSURLConnection asynchronous download and Encapsulation
# Import "ZJHttpRequest. h "// eliminate the warning of performSelector # pragma clang diagnostic ignored"-Warc-warning mselector-leaks "// class extension // Project Practice: // some instance variables are used internally, do not want to put it in the header file. Put it here @ 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] ;}} @ end
2. JSON format and JSON Format Parsing
JSON format:
{// {} Dictionary, [] array "list": [{"IsXuanZuo": 0, "Name ": "" Liang jingru · your name is love "2015 world tour concert Tianjin Station", "PriceStr": "299,399,599,799,999", "ProjectID": 78647, "ShowTime": "2015.07.25 ", "SiteStatus": 8, "Summary": "The barley APP started prepayment at, January 1, April 7! "," VenId ": 1403," VenName ":" Tianjin Sports Center (Grand Hall) "," cityname ":" Tianjin "," openSum ": 5836," priceName ": "299-999 yuan "},...}
The easiest way to parse JSON:
NSJSONSerialization
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.data options:NSJSONReadingMutableContainers error:nil];
3. data display and asynchronous image display using SDWebImage
SDWebImage -- UIImageView + WebCache. h
You need to set Memory Management --> Project ---> Build Phases --> set Compiler Flags for all SD files.
Content 1. Introduction to basic network download knowledge what is a network application? Network Application Program Structure
C/S structure: Cilent Client
Server
Common Data Formats
JSON/XML
General Interface Development Process
Interface Template --> data resource --> download data, parse --> Design Interface, refresh
2. Use NSURLConnection
NSString synchronization, NSURLConnection synchronization --> synchronization download Interface Card (generally not used)
NSURLConnection asynchronous
3. JSON format description and formatting Tool
4. Implementation of a complete page (including the creation of a model and the use of SDWebImage)
MVC mode --> M: model, V: view, C: controller
Download data --> parse --> extract data in model --> Create cell to set data