國家氣象局提供的天氣預報接口
10余年的來賓網站建設經驗,針對設計、前端、開發、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網營銷推廣的優勢是能夠根據用戶設備顯示端的尺寸不同,自動調整來賓建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優雅布局與設計,從而大程度地提升瀏覽體驗。成都創新互聯從事“來賓網站設計”,“來賓網站推廣”以來,每個客戶項目都認真落實執行。
接口地址有三個:
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.html
第三接口信息較為詳細,提供的是6天的天氣,關于API所返回的信息請見開源免費天氣預報接口API以及全國所有地區代碼!!(國家氣象局提供),全國各城市對應這一個id號,根據改變id好我們就可以解析出來各個城市對應天氣;
  
Json以其輕巧簡單成為較為流行文件格式,在手機上傳輸比XML快,iOS5以前蘋果公司并沒有對Json解析提供庫文件支持,但是好在有一些大牛們專門為Objective-c只做了能夠解析Json文件的庫,iOS蘋果公司提供了對json的原生支持類NSJSONSerialization;本文將介紹TouchJson SBJson 和iOS5所支持的原生的json方法,解析國家氣象局API,TouchJson和SBJson需要下載他們的庫
TouchJson http://download.csdn.net/detail/duxinfeng2010/4484144
SBJson http://download.csdn.net/detail/duxinfeng2010/4484842
  
  1.創建一個新工程叫JsonThreeDemo; File->New->Project   ->single View Application -> next,注意不使用ARC,不要勾選Use Automatic Refrence Counting,否則運行時候庫文件中會報錯
  
  
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"]);  } - (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"]);      }  - (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
4.運行結果(如果想知道每次字符串和字典間取值情況,只需NSLog打印輸出就行):

5.再解析取值的時候花費了一些時間,取值時發生應用程序崩潰,獲取值不正確
有時我們從字典中獲取了這樣的數據,感覺比較郁悶,并未顯示中文,這種情況是我們把數據放到字典中,編碼方式是UTF8,取值打印出來的時候就成中文了

在解析出來數據后我想這樣取值,
NSDictionary *weatherInfo = [rootDicobjectForKey:@"weatherinfo"];
NSArray *weatherArray = [rootDicobjectForKey:@"weatherinfo"];
for (NSDictionary *dicin weatherArray) {
NSLog(@"----->%@",dic);
}
打印出來的dic數據是這樣的
NSLog(@"----->%@",[dicobjectForKey:@"city"]);來取出city的值,但是應用程序崩潰

源代碼:http://download.csdn.net/detail/duxinfeng2010/4484818
                分享名稱:iOS三種Json方法解析國家氣象局API
                
                標題鏈接:http://www.yijiale78.com/article26/pceocg.html
            
成都網站建設公司_創新互聯,為您提供做網站、云服務器、App設計、用戶體驗、外貿網站建設、服務器托管
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯
