作為一種輕量級(jí)的數(shù)據(jù)交換格式,json正在逐步取代xml,成為網(wǎng)絡(luò)數(shù)據(jù)的通用格式。
有的json代碼格式比較混亂,可以使用此“
http://www.bejson.com/
”網(wǎng)站來(lái)進(jìn)行JSON格式化校驗(yàn)(
點(diǎn)擊打開鏈接
)。此網(wǎng)站不僅可以檢測(cè)Json代碼中的錯(cuò)誤,而且可以以視圖形式顯示json中的數(shù)據(jù)內(nèi)容,很是方便。
從IOS5開始,APPLE提供了對(duì)json的原生支持(
NSJSONSerialization
),但是為了兼容以前的ios版本,可以使用第三方庫(kù)來(lái)解析Json。
本文將介紹
TouchJSON、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法
,解析國(guó)家氣象局API。
國(guó)家氣象局提供的天氣預(yù)報(bào)接口
接口地址有三個(gè):
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所返回的信息請(qǐng)見
開源免費(fèi)天氣預(yù)報(bào)接口API以及全國(guó)所有地區(qū)代碼!!(國(guó)家氣象局提供)
,全國(guó)各城市對(duì)應(yīng)這一個(gè)id號(hào),根據(jù)改變id好我們就可以解析出來(lái)各個(gè)城市對(duì)應(yīng)天氣;
使用Cocoapods 將
TouchJSON、 SBJson 、JSONKit?
第三方的框架加入到項(xiàng)目中:
相關(guān)代碼如下: ?
?
#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 { //天氣預(yù)報(bào)的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); //將解析得到的內(nèi)容放到字典中,編碼格式為UTF8,防止取值的時(shí)候發(fā)生亂碼 NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error]; //因?yàn)榉祷氐膉son文件有兩層,將第二層的內(nèi)容顯示出來(lái) 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相當(dāng) - (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,性能最優(yōu) - (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
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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