iOS Web data download and JSON parsing

Source: Internet
Author: User

Introduction to iOS Web data download and JSON parsing in this article, I'm going to show you how to use nsurlconnection to download data from the Web, how to parse the downloaded JSON data format, and how to display the asynchronous download display of data and images. The knowledge points involved are: 1.NSURLConnection asynchronous download and encapsulation, 2.JSON format and JSON format parsing; 3. Data display and use Sdwebimage to display images asynchronously. Content 1. Introduction to basic knowledge of network download

What is a Web application?

The program structure of network application

Common forms of network interfaces

Common data formats

General flow of interface development

2 Nsurlconnection Use
#import "ZJHttpRequest.h"//Elimination of Performselector warnings#pragmaClang diagnostic ignored "-warc-performselector-leaks"//class Extension//Project Practice://Some instance variables are used internally and don't want to be placed in the header file.@interfaceZjhttprequest () <NSURLConnectionDataDelegate>{nsurlconnection*_connection; NSString*_url; ID_target; SEL _action;}@end@implementationzjhttprequest//function://incoming URL, download complete executing the action method in target object-(void) Requestwithurl: (nsstring *) URL target: (ID) Target action: (SEL) action{_url=URL; _target=Target; _action=Action; //initiating a 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{//The download is complete and the method of saving is executed .        if(_target &&[_target respondstoselector:_action])    {[_target performselector:_action withobject:self]; }}@end

3. JSON format description and formatting tools
- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.//Data Interfacensstring*urlstring=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; _dataarray=[[Nsmutablearray alloc]init]; //Download_request =[[Zjhttprequest alloc]init];        [_request requestwithurl:urlstring target:self Action: @selector (dealdownloadfinish:)];    [Self creattableview]; }-(void) creattableview{_tableview=[[UITableView alloc]initwithframe:self.view.bounds Style:uitableviewstyleplain]; _tableview.Delegate=Self ; _tableview.datasource=Self ; [Self.view Addsubview:_tableview];}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return_dataarray.count;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return  -;}-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNsstring*cellid=@"Cell"; Appcell*cell=[TableView Dequeuereusablecellwithidentifier:cellid]; if(Cell = =Nil) {Cell=[[Appcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellid]; } Appmodel*model=_dataarray[indexpath.row];//cell.textlabel.text= Model.name;cell.namelabel.text=Model.name; //load pictures from the network[Cell.iconimageview Setimagewithurl:[nsurl URLWITHSTRING:MODEL.ICONURL]; returncell;}-(void) Dealdownloadfinish: (zjhttprequest*) request{nsdictionary*dic=[nsjsonserialization JSONObjectWithData:request.data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@"dic=%@", DIC); Nsarray*applist=dic[@"Applications"];  for(nsdictionary*appdictinchapplist) {Appmodel*model=[[Appmodel alloc]init]; Model.applicationid=appdict[@"Application"]; Model.name=appdict[@"name"]; Model.iconurl=appdict[@"ICONURL"];            [_dataarray Addobject:model]; }//Download Data Refresh display[_tableview reloaddata];}

4. A complete page point implementation (including model creation, sdwebimage use)

iOS Web data download and JSON parsing

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.