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

來源:互聯網
上載者:User

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

  在本文中筆者將要給大家介紹IOS中如何利用URLConnection從網路上下載資料,如何解析下載下來的JSON資料格式,以及如何顯示資料和圖片的一部下載顯示

  涉及到的知識點:

  1.URLConnection非同步下載和封裝

  2.JSON格式和JSON格式解析

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

 

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

    需要串連網路的應用,例如:,QQ,足記等;

    而電腦,相機,日曆等不需要串連網路的稱為本地應用。

  網路應用的程式結構

    需要有用戶端和服務端

      服務端是為用戶端服務的,服務的內容諸如向用戶端提供資源,儲存用戶端資料。

      用戶端(Client)或稱為使用者端,是指與伺服器相對應,為客戶提供本地服務的程式

  常用的網路介面形式

    IOS網路應用常見的資料介面一半都是HTTP形式的URL地址;

    其他的網路資料介面 :http,ftp,https

  常見的資料格式

    IOS開發中常見的資料格式有兩種, 一種是JSON格式, 另外種是XML格式, 相對來說, JSON格式使用的比較多佔了75%,XML佔了20%,其餘的佔5%

  介面開發的一般流程 

    IOS中開發一個介面, 需要介面, 介面素材資源, 和網路介面

    開發的流程一般如下所示

    ***1, 下載資料

    ***2, 解析JSON或XML資料, 建立資料模型model

    ***3, 使用控制項顯示資料, 必要的時候定製視圖, 例如定製cell

2.URLConnection使用

  NSString 同步下載

 NSString *urlString = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";    NSLog(@"%@",urlString);//    下載資料//    通過地址產生NSURL對象    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(@"下載失敗");    }    //    項目中怎麼使用//    1.同步形式下載,initWithContentsOfURL下載完了之後才會返回造成介面假死,不能使用//    2.使用非同步下載(NSURLConnection 非同步下載)

   NSURLConnection同步下載

 NSString *urlString = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";    //    發送同步URL請求//    NSURLRequest URL請求    NSURL *url = [NSURL URLWithString:urlString];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    NSError *error = nil;    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];    if (error == nil) {        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];        NSLog(@"str = %@",str);    }    else{        NSLog(@"下載失敗");    }

 

   NSURLConnection非同步下載

-(void)testtestNSURLConnectionAsyncDownloadData{    NSString *urlString = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";    //    初始化    _data = [[NSMutableData alloc]init];//    發起一個非同步URL串連請求//    非同步: 執行了方法之後開始下載,立即返回,下載過程在後台(多線程)執行    _connection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self startImmediately:YES];    NSLog(@"initWithRequest執行完成");    }//代理方法:接收到伺服器的響應執行-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSLog(@"接收到伺服器的響應");}//代理方法:接收到資料的時候執行//注意:但資料比較大的時候可能多次執行-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [_data appendData:data];}//代理方法:資料下載完成了-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSString *str= [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];    NSLog(@"str = %@",str);    //    最簡單的現實名字//    解析JSON//    作用:JSON資料轉化為NSDictionary或NSArray    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];    NSArray *appList = dic[@"applications"];    for (NSDictionary *appDic in appList) {        NSLog(@"name = %@",appDic[@"name"]);    }}//代理方法:資料下載失敗-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"error = %@",error);}

 

3.JSON格式說明和格式化工具
{  "list" : [    {      "IsXuanZuo" : 0,      "Name" : "汪峰2014“峰暴來臨”超級巡迴演唱會 重慶站",      "PriceStr" : "180,280,380,480,680,980,1280",      "ProjectID" : 66611,      "ShowTime" : "2014.11.01",      "SiteStatus" : 8,      "Summary" : "【先付預告】2014年5月12日12:08開啟先付!",      "VenId" : 258,      "VenName" : "重慶奧體中心",      "cityname" : "重慶市",      "priceName" : "180-1280元"    },    {      "IsXuanZuo" : 0,      "Name" : "莫文蔚20周年巡迴演唱會 鄭州站",      "PriceStr" : "480,680,880,1280,1680,2014",      "ProjectID" : 66313,      "ShowTime" : "2014.07.26",      "SiteStatus" : 8,      "Summary" : "【用戶端專享】480元少量稀缺票用戶端獨家售賣!",      "VenId" : 942,      "VenName" : "鄭州國際會展中心",      "cityname" : "鄭州市",      "priceName" : "480-2014元"    },    {      "IsXuanZuo" : 0,      "Name" : "王力宏2014MUSIC-MANII“火力全開”世界巡迴演唱會—北京站",      "PriceStr" : "280,380,480,580,880,1280,1680",      "ProjectID" : 66052,      "ShowTime" : "2014.06.14",      "SiteStatus" : 8,      "Summary" : "坦克上台火力十足,打造音樂戰役!全新演繹經典作品,體驗超凡音樂魅力!",      "VenId" : 392,      "VenName" : "國家體育場",      "cityname" : "北京市",      "priceName" : "280-1680元"    }  ]}

      []    表示數組,對應NSArray

      ,     表示並列的資料

      {}    表示字典,對應NSDictionary

      :     表示索引值對

      "xxx"  表示字串,對應NSString

      20    對應NSNumber

    JSON格式格式化工具

        Jason

        Json Editor

        線上: http://www.kjson.com/

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

 

My Code:下載

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.