國家氣象局提供的天氣預(yù)報接口
莊河ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!接口地址有三個:
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
第三接口信息較為詳細(xì),提供的是6天的天氣,關(guān)于API所返回的信息請見開源免費天氣預(yù)報接口API以及全國所有地區(qū)代碼?。。▏覛庀缶痔峁?,全國各城市對應(yīng)這一個id號,根據(jù)改變id好我們就可以解析出來各個城市對應(yīng)天氣;
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.創(chuàng)建一個新工程叫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); // 將解析得到的內(nèi)容存放字典中,編碼格式UTF8,防止取值時候發(fā)生亂碼 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數(shù)據(jù)放到NSData對象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // iOS5自帶解析類NSJSONSerialization從response中解析出數(shù)據(jù)放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; // weatherDic字典中存放的數(shù)據(jù)也是字典型,從它里面通過鍵值取值 NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; NSLog(@"今天是 %@ %@ %@ 的天氣狀況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]); // 打印出weatherInfo字典所存儲數(shù)據(jù) NSLog(@"weatherInfo字典里面的內(nèi)容是--->%@",[weatherInfo description]); }
我們用到了這樣一個類方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
4.運行結(jié)果(如果想知道每次字符串和字典間取值情況,只需NSLog打印輸出就行):
5.再解析取值的時候花費了一些時間,取值時發(fā)生應(yīng)用程序崩潰,獲取值不正確
有時我們從字典中獲取了這樣的數(shù)據(jù),感覺比較郁悶,并未顯示中文,這種情況是我們把數(shù)據(jù)放到字典中,編碼方式是UTF8,取值打印出來的時候就成中文了
在解析出來數(shù)據(jù)后我想這樣取值,
NSDictionary *weatherInfo = [rootDicobjectForKey:@"weatherinfo"];
NSArray *weatherArray = [rootDicobjectForKey:@"weatherinfo"];
for (NSDictionary *dicin weatherArray) {
NSLog(@"----->%@",dic);
}
打印出來的dic數(shù)據(jù)是這樣的NSLog(@"----->%@",[dicobjectForKey:@"city"]);來取出city的值,但是應(yīng)用程序崩潰
源代碼:http://download.csdn.net/detail/duxinfeng2010/4484818
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:iOS三種Json方法解析國家氣象局API-創(chuàng)新互聯(lián)
URL地址:http://aaarwkj.com/article30/dohgso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站策劃、服務(wù)器托管、軟件開發(fā)、小程序開發(fā)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)