iOS Web data download and JSON parsing

Source: Internet
Author: User

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

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.