iOS三種Json方法解析國家氣象局API

國家氣象局提供的天氣預報接口html

接口地址有三個:json

http://www.weather.com.cn/data/sk/101010100.html數組

http://www.weather.com.cn/data/cityinfo/101010100.html服務器

http://m.weather.com.cn/data/101010100.htmlapp

第三接口信息較爲詳細,提供的是6天的天氣,關於API所返回的信息請見開源免費天氣預報接口API以及全國全部地區代碼!!(國家氣象局提供),全國各城市對應這一個id號,根據改變id好咱們就能夠解析出來各個城市對應天氣;ide


Json以其輕巧簡單成爲較爲流行文件格式,在手機上傳輸比XML快,iOS5之前蘋果公司並無對Json解析提供庫文件支持,可是好在有一些大牛們專門爲Objective-c只作了可以解析Json文件的庫,iOS蘋果公司提供了對json的原生支持類NSJSONSerialization;本文將介紹TouchJson SBJson 和iOS5所支持的原生的json方法,解析國家氣象局API,TouchJson和SBJson須要下載他們的庫 編碼

TouchJson  http://download.csdn.net/detail/duxinfeng2010/4484144url

SBJson     http://download.csdn.net/detail/duxinfeng2010/4484842spa


1.建立一個新工程叫JsonThreeDemo; File->New->Project ->single View Application -> next,注意不使用ARC,不要勾選Use Automatic Refrence Counting,不然運行時候庫文件中會報錯
.net



2.使用TouchJson庫須要添加頭文件 #import "CJSONDeserializer.h",使用SBJson須要添加頭文件 #import "SBJson.h"而後打開XIB添加三個button,讓添加三個方法


- (IBAction)buttonPressedone:(id)sender;

- (IBAction)buttonPressedtwo:(id)sender;

- (IBAction)buttonPressedthree:(id)sender;


3.三個解析方法都相似

TouchJson庫解析北京天氣

- (IBAction)buttonPressedone:(id)sender { //    獲取API接口     NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"]; //    定義一個NSError對象,用於捕獲錯誤信息     NSError *error; //         NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];     //    NSLog(@"jsonstring--->%@",jsonString); //    將解析獲得的內容存放字典中,編碼格式UTF8,防止取值時候發生亂碼     NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error]; //    由於返回的Json文件有兩層,去第二層類容放到字典中去0     NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; //    取值打印     NSLog(@"今天是 %@ %@ %@ 的天氣情況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);  }


SBJson庫,解析南陽天氣,換一下城市的id號就能夠了

- (IBAction)buttonPressedtwo:(id)sender {     NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];     NSError *error=nil;     NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];       SBJsonParser *parser = [[SBJsonParser alloc]init];          NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];     NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];     NSLog(@"今天是 %@ %@ %@ 的天氣情況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);      } 


iOS5所支持的原生json解析,信陽市天氣
- (IBAction)buttonPressedthree:(id)sender {     NSError *error; //    加載一個NSURL對象     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]]; //    將請求的url數據放到NSData對象中     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //    iOS5自帶解析類NSJSONSerialization從response中解析出數據放到字典中     NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; //    weatherDic字典中存放的數據也是字典型,從它裏面經過鍵值取值     NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];          NSLog(@"今天是 %@ %@ %@ 的天氣情況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]); //    打印出weatherInfo字典所存儲數據     NSLog(@"weatherInfo字典裏面的內容是--->%@",[weatherInfo description]); }

若是咱們像獲取更多信息,直接從字典中取值

咱們用到了這樣一個類方法

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

  • request 要裝載的URL請求. 這個request 對象 做爲初始化進程的一部分,被深度複製(deep-copied). 在這個方法返回以後, 再修改request, 將不會影響用在裝載的過程當中的request
  • reponse 輸出參數, 由服務器返回的URL響應
  • error   輸出參數, 若是在處理請求的過程當中發生錯誤,就會使用.  無錯誤,就爲NULL
它返回的是一個下載的url請求,若是鏈接失敗或者建立失敗失敗返回nil


4.運行結果(若是想知道每次字符串和字典間取值狀況,只需NSLog打印輸出就行):



5.再解析取值的時候花費了一些時間,取值時發生應用程序崩潰,獲取值不正確

有時咱們從字典中獲取了這樣的數據,感受比較鬱悶,並未顯示中文,這種狀況是咱們把數據放到字典中,編碼方式是UTF8,取值打印出來的時候就成中文了



在解析出來數據後我想這樣取值

NSDictionary *weatherInfo = [rootDicobjectForKey:@"weatherinfo"];

    NSArray *weatherArray = [rootDicobjectForKey:@"weatherinfo"];

    for (NSDictionary *dicin weatherArray) {

        NSLog(@"----->%@",dic);

    }

打印出來的dic數據是這樣的


這是咱們json文件的第二層數據取出放到了一個數組中,而後定義了一個字典對象在數組中遍歷取出存放的數據,因而就想用

NSLog(@"----->%@",[dicobjectForKey:@"city"]);來取出city的值,可是應用程序崩潰


出現這種狀況是由於在對解析出數據存值和取值發生問題,說明這種方式是取值是不正確的;


源代碼:http://download.csdn.net/detail/duxinfeng2010/4484818

相關文章
相關標籤/搜索