iOS三种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好我们就可以解析出来各个城市对应天气;


Json以其轻巧简单成为较为流行文件格式,在手机上传输比XML快,iOS5以前苹果公司并没有对Json解析提供库文件支持,但是好在有一些大牛们专门为Objective-c只做了能够解析Json文件的库,iOS苹果公司提供了对json的原生支持类NSJSONSerialization;本文将介绍TouchJson SBJson 和iOS5所支持的原生的json方法,解析国家气象局API,TouchJson和SBJson需要下载他们的库 

TouchJson  http://download.csdn.net/detail/duxinfeng2010/4484144

SBJson     http://download.csdn.net/detail/duxinfeng2010/4484842


1.创建一个新工程叫JsonThreeDemo; File->New->Project ->single View Application -> next,注意不使用ARC,不要勾选Use Automatic Refrence Counting,否则运行时候库文件中会报错



2.使用TouchJson库需要添加头文件 #import "CJSONDeserializer.h",使用SBJson需要添加头文件 #import "SBJson.h"然后打开XIB添加三个button,让添加三个方法


- (IBAction)buttonPressedone:(id)sender;

- (IBAction)buttonPressedtwo:(id)sender;

- (IBAction)buttonPressedthree:(id)sender;


3.三个解析方法都类似

TouchJson库解析北京天气

- (IBAction)buttonPressedone:(id)sender { //    获取API接口     NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"]; //    定义一个NSError对象,用于捕获错误信息     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文件有两层,去第二层类容放到字典中去0     NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; //    取值打印     NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);  }


SBJson库,解析南阳天气,换一下城市的id号就可以了

- (IBAction)buttonPressedtwo:(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 error:&error];     NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];     NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);      } 


iOS5所支持的原生json解析,信阳市天气
- (IBAction)buttonPressedthree:(id)sender {     NSError *error; //    加载一个NSURL对象     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]]; //    将请求的url数据放到NSData对象中     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //    iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中     NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; //    weatherDic字典中存放的数据也是字典型,从它里面通过键值取值     NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];          NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]); //    打印出weatherInfo字典所存储数据     NSLog(@"weatherInfo字典里面的内容是--->%@",[weatherInfo description]); }

如果我们像获取更多信息,直接从字典中取值

我们用到了这样一个类方法

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

  • request 要装载的URL请求. 这个request 对象 作为初始化进程的一部分,被深度复制(deep-copied). 在这个方法返回之后, 再修改request, 将不会影响用在装载的过程中的request
  • reponse 输出参数, 由服务器返回的URL响应
  • error   输出参数, 如果在处理请求的过程中发生错误,就会使用.  无错误,就为NULL
它返回的是一个下载的url请求,如果连接失败或者创建失败失败返回nil


4.运行结果(如果想知道每次字符串和字典间取值情况,只需NSLog打印输出就行):



5.再解析取值的时候花费了一些时间,取值时发生应用程序崩溃,获取值不正确

有时我们从字典中获取了这样的数据,感觉比较郁闷,并未显示中文,这种情况是我们把数据放到字典中,编码方式是UTF8,取值打印出来的时候就成中文了



在解析出来数据后我想这样取值



NSDictionary *weatherInfo = [rootDicobjectForKey:@"weatherinfo"];

    NSArray *weatherArray = [rootDicobjectForKey:@"weatherinfo"];

    for (NSDictionary *dicin weatherArray) {

        NSLog(@"----->%@",dic);

    }

打印出来的dic数据是这样的


这是我们json文件的第二层数据取出放到了一个数组中,然后定义了一个字典对象在数组中遍历取出存放的数据,于是就想用

NSLog(@"----->%@",[dicobjectForKey:@"city"]);来取出city的值,但是应用程序崩溃


出现这种情况是因为在对解析出数据存值和取值发生问题,说明这种方式是取值是不正确的;


源代码:http://download.csdn.net/detail/duxinfeng2010/4484818



     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208785,如需转载请自行联系原作者




相关文章
|
11天前
|
JSON JavaScript 前端开发
JavaScript原生代码处理JSON的一些高频次方法合集
JavaScript原生代码处理JSON的一些高频次方法合集
|
25天前
|
机器学习/深度学习 存储 PyTorch
Pytorch中in-place操作相关错误解析及detach()方法说明
Pytorch中in-place操作相关错误解析及detach()方法说明
41 0
|
24天前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
255 0
|
7天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
21 0
|
16天前
|
存储 缓存 监控
深入解析linux内存指标:快速定位系统内存问题的有效技巧与实用方法(free、top、ps、vmstat、cachestat、cachetop、sar、swap、动态内存、cgroops、oom)
深入解析linux内存指标:快速定位系统内存问题的有效技巧与实用方法(free、top、ps、vmstat、cachestat、cachetop、sar、swap、动态内存、cgroops、oom)
|
16天前
|
存储 算法
从动态规划到贪心算法:最长递增子序列问题的方法全解析
从动态规划到贪心算法:最长递增子序列问题的方法全解析
16 2
|
23天前
|
算法 项目管理 开发者
【Conan 入门教程 】深入解析Conan中的依赖关系的定义方法(In-depth Analysis of Dependency Definition Methods in Conan)
【Conan 入门教程 】深入解析Conan中的依赖关系的定义方法(In-depth Analysis of Dependency Definition Methods in Conan)
35 0
|
25天前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
79 2
|
25天前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
43 0
|
27天前
|
安全 数据安全/隐私保护 虚拟化
iOS应用加固方案解析:ipa加固安全技术全面评测
iOS应用加固方案解析:ipa加固安全技术全面评测
36 3

推荐镜像

更多