交互数据解析之JSON解析

本文介绍并演示了使用NSJSONSerialization解析JSON数据的过程,包括从国家气象局获取天气预报数据,并展示了如何提取关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用的交互数据格式XML和JSON,此篇来说JSON解析

JSON 解析的普遍4种方式

  1. TouchJson
  2. SBJson
  3. JSONKit
  4. NSJSONSerialization(Avaliable iOS 5 and later)

上面的4种解析 性能从上到下更好,也就是苹果自带的NSJSONSerialization性能最好。

实例: JSON解析国家气象局提供的天气预报

天气接口:
http://m.weather.com.cn/data/101010100.html

详细内容可以参照http://blog.youkuaiyun.com/duxinfeng2010/article/details/7830136

JSON北京的天气数据:

{“weatherinfo”:{“city”:”北京”,”city_en”:”beijing”,”date_y”:”2014年3月4日”,”date”:”“,”week”:”星期二”,”fchh”:”11”,”cityid”:”101010100”,”temp1”:”8℃~-3℃”,”temp2”:”8℃~-3℃”,”temp3”:”7℃~-3℃”,”temp4”:”8℃~-1℃”,”temp5”:”10℃~1℃”,”temp6”:”10℃~2℃”,”tempF1”:”46.4℉~26.6℉”,”tempF2”:”46.4℉~26.6℉”,”tempF3”:”44.6℉~26.6℉”,”tempF4”:”46.4℉~30.2℉”,”tempF5”:”50℉~33.8℉”,”tempF6”:”50℉~35.6℉”,”weather1”:”晴”,”weather2”:”晴”,”weather3”:”晴”,”weather4”:”晴转多云”,”weather5”:”多云”,”weather6”:”多云”,”img1”:”0”,”img2”:”99”,”img3”:”0”,”img4”:”99”,”img5”:”0”,”img6”:”99”,”img7”:”0”,”img8”:”1”,”img9”:”1”,”img10”:”99”,”img11”:”1”,”img12”:”99”,”img_single”:”0”,”img_title1”:”晴”,”img_title2”:”晴”,”img_title3”:”晴”,”img_title4”:”晴”,”img_title5”:”晴”,”img_title6”:”晴”,”img_title7”:”晴”,”img_title8”:”多云”,”img_title9”:”多云”,”img_title10”:”多云”,”img_title11”:”多云”,”img_title12”:”多云”,”img_title_single”:”晴”,”wind1”:”北风4-5级转微风”,”wind2”:”微风”,”wind3”:”微风”,”wind4”:”微风”,”wind5”:”微风”,”wind6”:”微风”,”fx1”:”北风”,”fx2”:”微风”,”fl1”:”4-5级转小于3级”,”fl2”:”小于3级”,”fl3”:”小于3级”,”fl4”:”小于3级”,”fl5”:”小于3级”,”fl6”:”小于3级”,”index”:”寒冷”,”index_d”:”天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。”,”index48”:”冷”,”index48_d”:”天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。”,”index_uv”:”中等”,”index48_uv”:”中等”,”index_xc”:”较适宜”,”index_tr”:”一般”,”index_co”:”较舒适”,”st1”:”7”,”st2”:”-3”,”st3”:”8”,”st4”:”0”,”st5”:”7”,”st6”:”-1”,”index_cl”:”较不宜”,”index_ls”:”基本适宜”,”index_ag”:”易发”}}

使用NSJSONSerialization解析

- (void)useJSONSerialization:(NSURL *)url
{
    NSURLRequest *weatherRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
    [NSURLConnection sendAsynchronousRequest:weatherRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
    {

        if (connectionError)
        {
            NSLog(@"获取失败");
        }
        else
        {
            NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; //未作出错处理
            NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];

            NSLog(@"%@",weatherInfo);
            NSString *city = [weatherInfo objectForKey:@"city"];
            NSLog(@"%@",city);
        }
    }];
}

更多移步Github看实现原文件

JSONKit: https://github.com/johnezang/JSONKit

SBJSON: https://github.com/groopd/SBJSON-library

TouchJSON: https://github.com/TouchCode/TouchJSON

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值