IOS network data download and JSON parsing, and ios data json Parsing

Source: Internet
Author: User

IOS network data download and JSON parsing, and ios data json Parsing
IOS network data download and JSON Parsing

 

Introduction

In this article, I will introduce how to use NSURLconnection to download data from the network, parse the downloaded JSON data format, and display data and the asynchronous download and display of images.

Knowledge points involved;

1. NSURLconnection asynchronous download and Encapsulation

2. JSON format and JSON Format Parsing

3. data display and asynchronous image display using SDWebImage

Content

1. Introduction to basic network download knowledge

What is a network application?

Network applications are some applications that can be used normally in a network environment.

Network Application Program Structure

C/S structure

 

Client: displays data and communicates with users.

 

Server: provides data and services for clients.

General Interface Development Process

1. you first need to know what effect your own interface is, and then you can see what materials and network interfaces you need.

2. Use NSURLConnection to asynchronously download data.

3. parse the data to create a data model.

4. Define a UITabBarViewCell to display the interface.

 

2. Use of NSURLconnection

Here we encapsulate this asynchronous download method.

@interface ZXHttpRequest()<NSURLConnectionDataDelegate>{    NSString *_url;    SEL _action;    NSURLConnection *_connection;    id _target;}@implementation ZXHttpRequest-(void)requestWithUrl:(NSString *)url               target:(id)target               action:(SEL)action;{        _url=url;    _action=action;    _target=target;    _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{    [_target performSelector:_action withObject:self];}

 

 

 

NSString synchronous download data

We will write it in this method. The data interface is the data interface with no restrictions, and we cannot use synchronous download, because if you want to download a large amount of data, it is easy to get stuck in this method to upload, and our asynchronous download is run and downloaded in the background, however, synchronous download returns the method after the data is downloaded and then jumps out. This is their difference, and it is also a fatal flaw in synchronous download.

-(Void) testNSStringDownloadData {NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; 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 ");}}

 

 

3. JSON format description and formatting Tool

The most important thing about the JSON format is to figure out the nested relationship in its data. The method it parses is actually very simple, and the key is not the method, but the nested relationship, we can clearly see the nested Data Structure in JASON's parsing software and recommend it to you.

This is the resolution method:

 

    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:(NSData *)options:(NSJSONReadingOptions) error:(NSError *__autoreleasing *)                     

 

 

 

4. Implementation of a complete interface (including Model creation)

:

 

 

 

 

 

 

 

 

 

 

 

  

Related Article

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.