iOS網路資料下載和JSON解析,ios網路json解析

來源:互聯網
上載者:User

iOS網路資料下載和JSON解析,ios網路json解析
iOS網路資料下載和JSON解析簡介

  在本文中筆者將要給大家介紹iOS中如何利用NSURLConnection如何從網路中下載資料,如何解析下載下來的JSON資料格式,以及如何顯示資料和圖片的非同步下載顯示。

  涉及到的知識點:

  1.NSURLConnection非同步下載和封裝

#import "ZJHttpRequest.h"//消除performSelector的警告#pragma clang diagnostic ignored "-Warc-performSelector-leaks"//類擴充//項目實踐://  有些執行個體變數內部使用, 不想放在標頭檔中, 放在這兒@interface ZJHttpRequest ()<NSURLConnectionDataDelegate>{    NSURLConnection *_connection;    NSString *_url;    id _target;    SEL _action;}@end@implementation ZJHttpRequest//作用://  傳入網址, 下載完成執行後執行target對象中action方法-(void)requestWithUrl:(NSString *)url               target:(id)target               action:(SEL)action{    _url = url;    _target = target;    _action = action;        //發起URL請求    _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{    //下載完成了, 執行儲存的方法    if(_target && [_target respondsToSelector:_action])    {        [_target performSelector:_action withObject:self];    }    }@end

  2.JSON格式和JSON格式解析

    JSON格式:

{        //{}字典 ,[]數組  "list" : [    {      "IsXuanZuo" : 0,      "Name" : "“梁靜茹·你的名字是愛情”2015世界巡迴演唱會天津站",      "PriceStr" : "299,399,599,799,999",      "ProjectID" : 78647,      "ShowTime" : "2015.07.25",      "SiteStatus" : 8,      "Summary" : "4月7日13:00大麥APP搶先開啟先付預售!",      "VenId" : 1403,      "VenName" : "天津體育中心(大館)",      "cityname" : "天津市",      "openSum" : 5836,      "priceName" : "299-999元"    },    ...}

    JSON最簡便的解析方法:

  NSJSONSerialization
   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.data options:NSJSONReadingMutableContainers error:nil];

 

  3.資料顯示和使用SDWebImage非同步顯示圖片

    SDWebImage  -- UIImageView+WebCache.h  

    需設定記憶體管理  --> Project --->Build Phases  -->將所有SD檔案設定Compiler Flags

    

內容1.網路下載基礎知識介紹  什麼是網路應用?  網路應用的程式結構

   C/S結構:Cilent用戶端

        Server 服務端 

  常見的資料格式

    JSON/XML

  介面開發的一般流程

    介面模板--> 資料資源--> 下載資料,解析-->設計介面,重新整理

2.NSURLConnection使用

  NSString同步,NSURLConnection同步  --> 同步下載介面會卡(一般不用)

  NSURLConnection非同步

3.JSON格式說明和格式化工具

 

4.一個完整頁面的實現(包含model的建立,SDWebImage的使用)

   MVC模式 -->M:model  , V:view  , C:controller    

  資料下載--> 解析 --> 提取資料於model中 --> 建立cell 設定資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.