iOS network data download and JSON parsing

Source: Internet
Author: User

Introduction to iOS network data download and JSON parsing

In this article, I will give you an introduction to how iOS uses nsurlconnection to download data from the network, how to parse the downloaded JSON data format, and how to display the asynchronous download display of data and images.

The knowledge points involved:

1.NSURLConnection Asynchronous Download and encapsulation

#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 URLs, executing the action method in the target object after the download is complete-(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

2.JSON format and JSON format parsing

JSON format:

{//{} dictionary, [] array"List" : [    {      "Isxuanzuo":0,      "Name":""Fish Leong 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":"April 7 13:00 Barley app first open pre-paid presale! ",      "Venid":1403,      "Venname":"Tianjin Sports Center (Grand Hall)",      "CityName":"Tianjin City",      "Opensum":5836,      "Pricename":"299-999 USD"    },    ...}

The simplest parsing method of JSON:

Nsjsonserialization
Nsdictionary *dict = [Nsjsonserialization JSONObjectWithData:request.data options:nsjsonreadingmutablecontainers Error:nil];

3. Data display and use Sdwebimage to display images asynchronously

Sdwebimage--Uiimageview+webcache.h

Need to set memory management--Project--->build phases---Set all SD files compiler Flags

    

Content 1. Network Download Basics What is a Web application? The program structure of network application

c/S structure: Cilent client

Server Service Side

Common data formats

Json/xml

General flow of interface development

Interface template--Data resources--download data, parse--design interface, refresh

2.NSURLConnection use

NSString Sync, nsurlconnection Sync-to-Sync download interface card (usually not used)

Nsurlconnection Asynchronous

3.JSON format description and Formatting tools

4. Implementation of a complete page (including model creation, use of sdwebimage)

MVC pattern-->m:model, V:view, C:controller

Data download--parse--extract data in model--Create cell settings data

iOS network 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.