Introduction to iOS Web data download and JSON parsing
In this paper, I will show you how iOS uses nsurlconnection to download data from the Web, how to parse the downloaded Jsson data format, and how to display a download display of data and images.
The knowledge points involved:
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?
A Web application is an application that operates on the Internet or intranet using a Web browser. An application that is written in a Web page language (such as HTML, JavaScript, Java, and other programming languages) that needs to be run through a browser.
The program structure of network application: c/s,b/s;
Common form of Network interface: Ethernet interface;
Common data formats: Jason,xml;
2.NSURLConnection use
NSString Download Data synchronously ...
Use the URL address/HTTP/HTTP address using Protocol (ftp://) // iappfree.candou.com host address (domain name and IP) // : 8080 host port // /free/applications/limited Web program file path // ?currency=rmb&page=1& Category_id= program parameters (Parameters & Split) nsstring *urlstring = @ "http://iappfree.candou.com:8080/free/applications/ Limited?currency=rmb&page=1&category_id= "; Download data //Generate Nsurl object via address 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"); } How to use//1 in the project . Synchronous form Download, initwithcontentsofurl after downloading will not return // cause the interface suspended animation, can not use //2. Use asynchronous Download ( Nsurlconnection asynchronous download)
3.JSON parsing
Can be done by a system-provided class nsjsonserialization, can be easily converted into a dictionary or array. The sample code is as follows.
-(void) Dealdownloadfinish: (Nlhttprequest *) request{ nsdictionary *dict = [nsjsonserialization JSONObjectWithData:request.data options:nsjsonreadingmutablecontainers Error:nil]; Nsarray * dicts = [dict valueforkeypath:@ "List"]; For (Nsdictionary *dict in dicts) { nlevent *event = [Nlevent eventwithdictionary:dict]; [_modelarray addobject:event]; } [_tableview Reloaddata];}
iOS Web data download and JSON parsing