亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

JSON 數據的4中解析方式比較

系統 1616 0

作為一種輕量級的數據交換格式,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? 第三方的框架加入到項目中:

JSON 數據的4中解析方式比較
?

相關代碼如下: ?

?

      
#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

    

?

?

JSON 數據的4中解析方式比較


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲精品久久久久福利网站 | 国产成人亚洲精品一区二区在线看 | 91久久线看在观草草青青 | 亚洲国产成人久久一区www妖精 | 福利视频在线观看午夜 | 免费看日韩欧美一级毛片 | 欧美人成一本免费观看视频 | 久久中文字幕在线观看 | 免费在线观看黄色小视频 | www色中色| 久久久久免费观看 | 午夜美女久久久久爽久久 | 99国产精品久久久久久久成人热 | 玖玖爱免费 | 日本aⅴ网站 | 狠狠色噜噜狠狠狠合久 | 日韩在线 | 中文 | 国产专区在线 | 男人的天堂一区二区视频在线观看 | 国产成人亚洲欧美三区综合 | 爱爱视频在线观看 | www.久久精品视频 | 日日爱影院| 2020年新四虎免费 | 激情五月五月婷婷 | 在线观看久久 | 亚洲国产97在线精品一区 | 欧美一区中文字幕 | 视频在线一区二区三区 | 狠狠色噜噜狠狠狠狠五月婷 | 久操中文在线 | 亚洲精品亚洲人成人网 | 日日干日日操 | 免费看一级特黄a大片 | 免费福利在线视频 | 婷综合| swag国产精品 | jizzjizz欧美69巨大 | 欧洲视频一区 | 狠狠色成人综合 | 中文字幕在线观看2023 |