iOS学习笔记(十一)——JSON数据解析
2015-07-10 10:52
591 查看
在之前的《iOS学习——xml数据解析(九)》介绍了xml数据解析,这一篇简单介绍一下Json数据解析。JSON
即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与客户端的交互,Json语法参考。关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也是比其他第三方开源项目的高很多,详情可看下图。
图片详情可查看
NSJSONSerialization提供了Json数据封包、Json数据解析,NSJSONSerialization将JSON数据转换为NSDictionary或NSArray解包方法,将NSDictionary、NSArray对象转换为JSON数据(可以通过调用isValidJSONObject来判断NSDictionary、NSArray对象是否可以转换为JSON数
据)封包。这一篇将做简单介绍。
view plaincopyprint?
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];
// isValidJSONObject判断对象是否可以构建成json对象
if ([NSJSONSerialization isValidJSONObject:dic]){
NSError *error;
// 创造一个json从Data, NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json data:%@",json);
}
view plaincopyprint?
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
NSString *text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo:%@", text );
/**
* @author 张兴业
* http://blog.csdn.net/xyz_lmn
* iOS入门群:83702688
* android开发进阶群:241395671
* 我的新浪微博:@张兴业TBOW
*/
参考:
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946-CH1-DontLinkElementID_1 http://www.w3school.com.cn/json/ http://www.ibm.com/developerworks/cn/web/wa-lo-json/ http://blog.sina.com.cn/s/blog_7018d3820101bdqz.html
即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与客户端的交互,Json语法参考。关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也是比其他第三方开源项目的高很多,详情可看下图。
图片详情可查看
NSJSONSerialization提供了Json数据封包、Json数据解析,NSJSONSerialization将JSON数据转换为NSDictionary或NSArray解包方法,将NSDictionary、NSArray对象转换为JSON数据(可以通过调用isValidJSONObject来判断NSDictionary、NSArray对象是否可以转换为JSON数
据)封包。这一篇将做简单介绍。
Json数据封包
[cpp]view plaincopyprint?
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];
// isValidJSONObject判断对象是否可以构建成json对象
if ([NSJSONSerialization isValidJSONObject:dic]){
NSError *error;
// 创造一个json从Data, NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json data:%@",json);
}
Json数据解析
[cpp]view plaincopyprint?
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
NSString *text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo:%@", text );
/**
* @author 张兴业
* http://blog.csdn.net/xyz_lmn
* iOS入门群:83702688
* android开发进阶群:241395671
* 我的新浪微博:@张兴业TBOW
*/
参考:
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946-CH1-DontLinkElementID_1 http://www.w3school.com.cn/json/ http://www.ibm.com/developerworks/cn/web/wa-lo-json/ http://blog.sina.com.cn/s/blog_7018d3820101bdqz.html
相关文章推荐
- iOS开发-Xcode Debug、Release、Archive、Profile、Analyze概念解释
- IOS 开发如何获取键盘高度
- iOS应用程序声明周期
- JS与IOS互调用
- Xcode Archive 出现 Your Account Already Has a Valid iOS Distribution Certificate 警告
- iOS 7原生二维码扫描中文gbk编码乱码的解决
- iOS编程 界面布局 纯代码 VFL编写和添加 左右两个view,按比例设置大小
- iOS开发必看的博客汇总
- iOS将颜色转换image方法
- iOS编程 界面布局 纯代码 VFL编写和添加 左右两个view
- ios_KVO 基本用法 代码
- iOS编程 界面布局 纯代码 VFL编写和添加 上下两个view
- 【原】Github系列之二:开源 一行代码实现多形式多动画的推送小红点WZLBadge(iOS)
- ios项目(十六)
- iOS 类似QQ空间表视图下拉头部视图放大效果实现
- iOS 开发中问题 ——使用xib拖拽控件操作其约束并添加动画没有效果的问题
- iOS编程 界面布局 纯代码 VFL编写和添加
- iOS 8 模拟器上设置中文输入
- IOS下获取时间、手机系统时区以及获取时间间隔
- IOS图片压缩上传服务器终极解决方案