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

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條評論
主站蜘蛛池模板: 夜夜撸日日干 | 中文字幕专区 | 国产精品va| 欧美大色网 | 毛片a| 香蕉精品高清在线观看视频 | 国产a一级毛片午夜剧场14 | 福利入口在线观看 | 四虎国产永久免费久久 | 99精品国产在现线免费 | 97久久曰曰久久久 | 狠狠色婷婷综合天天久久丁香 | 国产精品爱久久久久久久小 | 国产精品小视频在线观看 | 天天干夜夜夜 | 久久天堂一区二区三区 | 国产精品福利在线观看 | 99国产超薄丝袜足j在线观看 | 欧美一级夜夜爽 视频 | 国产―笫一页―浮力影院xyz | 成人久久免费视频 | 国产黄网永久免费 | 97精品视频在线 | 模特视频一二三区 | 五月天精品 | 久久激情免费视频 | 日韩视频不卡 | 激情综合网五月激情 | 欧美日韩大尺码免费专区 | 欧美精品一区二区三区在线 | 亚洲色图国产精品 | 国产精品久久亚洲不卡4k岛国 | 成人免费一区二区三区在线观看 | 亚洲欧美日韩国产综合高清 | 中文字幕亚洲 综合久久 | 国产99网站| 女人18女人毛片 | 久久国产乱子伦精品在 | 天天干夜操 | 亚洲精品视频一区二区 | 亚洲一区中文字幕 |