作為一種輕量級的數據交換格式,json正在逐步取代xml,成為網絡數據的通用格式。
有的json代碼格式比較混亂,可以使用此“
http://www.bejson.com/
”網站來進行JSON格式化校驗(
點擊打開鏈接
)。此網站不僅可以檢測Json代碼中的錯誤,而且可以以視圖形式顯示json中的數據內容,很是方便。
從IOS5開始,APPLE提供了對json的原生支持(
NSJSONSerialization
),但是為了兼容以前的ios版本,可以使用第三方庫來解析Json。
本文將介紹
TouchJSON、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法
,解析國家氣象局API。
國家氣象局提供的天氣預報接口
接口地址有三個:
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好我們就可以解析出來各個城市對應天氣;
使用Cocoapods 將
TouchJSON、 SBJson 、JSONKit?
第三方的框架加入到項目中:
相關代碼如下: ?
?
#import "ViewController.h" #import <SBJson/SBJson.h> #import <TouchJSON/CJSONDeserializer.h> #import <JSONKit/JSONKit.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } //TouchJSON解析json,性能最差 - (IBAction)touchJSON:(id)sender { //天氣預報的url NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"]; 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文件有兩層,將第二層的內容顯示出來 NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; self.weatherMessage.text = [NSString stringWithFormat:@"今天是%@ %@ %@ 的天氣狀況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]]; } * //SBJson解析json,性能倒二 - (IBAction)SBJson:(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]; NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; self.weatherMessage.text = [NSString stringWithFormat:@"今天是%@ %@ %@ 的天氣狀況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]]; } //JSONKit解析json,性能第二,與iosJSON相當 - (IBAction)JSONKit:(id)sender { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180801.html"]; NSError *error = nil; NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKSerializeOptionNone]; id result = [decoder objectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]; if ([result isKindOfClass:[NSDictionary class]]) { NSDictionary *rootDic = (NSDictionary *)result; NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; self.weatherMessage.text = [NSString stringWithFormat:@"今天是%@ %@ %@ 的天氣狀況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]]; } } //ios5 自帶的JSON器解析json,性能最優 - (IBAction)iosJSON:(id)sender { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]; NSError *error = nil; NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; id result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error]; if ([result isKindOfClass:[NSDictionary class]]) { NSDictionary *rootDic = (NSDictionary *)result; NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; self.weatherMessage.text = [NSString stringWithFormat:@"今天是%@ %@ %@ 的天氣狀況是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]]; } } @end
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
