使用"JSONKit.h"解析文件
2014-04-01 18:23
531 查看
使用"JSONKit.h"解析的步骤:
1 获取文件地址
2 将文件内容赋给字符串
3 将字符串转换为NSData
4 jiangNSData转换为字典
5 使用键值取出字典的内容
代码:
NSString * resourcePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"];
NSString * strJson = [NSString stringWithContentsOfFile:resourcePath encoding:NSUTF8StringEncoding error:NULL];
NSData* jsonData = [strJson dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* resultDic = [jsonData objectFromJSONData];
NSLog(@"resultDic = %@",resultDic);
NSDictionary * weatherinfoArray = [resultDic objectForKey:@"weatherinfo"];
NSLog(@"weatherinfoArray = %@",weatherinfoArray);
NSString * w1city = [weatherinfoArray objectForKey:@"city"];
self.textCity.text = w1city;
NSLog(@"w1city = %@",w1city);
NSString * w1city_en = [weatherinfoArray objectForKey:@"city_en"];
self.textPinyin.text = w1city_en;
NSLog(@"w1city_en = %@",w1city_en);
NSString * w1date_y = [weatherinfoArray objectForKey:@"date_y"];
self.textDate.text = w1date_y;
NSLog(@"w1date_y = %@",w1date_y);
NSString * w1img_title2 = [weatherinfoArray objectForKey:@"img_title2"];
self.teatWeacher.text = w1img_title2;
NSLog(@"w1img_title2 = %@",w1img_title2);
1 获取文件地址
2 将文件内容赋给字符串
3 将字符串转换为NSData
4 jiangNSData转换为字典
5 使用键值取出字典的内容
代码:
NSString * resourcePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"];
NSString * strJson = [NSString stringWithContentsOfFile:resourcePath encoding:NSUTF8StringEncoding error:NULL];
NSData* jsonData = [strJson dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* resultDic = [jsonData objectFromJSONData];
NSLog(@"resultDic = %@",resultDic);
NSDictionary * weatherinfoArray = [resultDic objectForKey:@"weatherinfo"];
NSLog(@"weatherinfoArray = %@",weatherinfoArray);
NSString * w1city = [weatherinfoArray objectForKey:@"city"];
self.textCity.text = w1city;
NSLog(@"w1city = %@",w1city);
NSString * w1city_en = [weatherinfoArray objectForKey:@"city_en"];
self.textPinyin.text = w1city_en;
NSLog(@"w1city_en = %@",w1city_en);
NSString * w1date_y = [weatherinfoArray objectForKey:@"date_y"];
self.textDate.text = w1date_y;
NSLog(@"w1date_y = %@",w1date_y);
NSString * w1img_title2 = [weatherinfoArray objectForKey:@"img_title2"];
self.teatWeacher.text = w1img_title2;
NSLog(@"w1img_title2 = %@",w1img_title2);
相关文章推荐
- 对使用"ALTER DATABASE BACKUP CONTROLFILE TO ''" 备份的控制文件的恢
- 网页速度分析 && 网页头文件解析 && 性能优化 && httpwatch工具使用
- 使用fsck命令修复linux文件系统错误【避开引起系统崩溃,启动不了的"-a"选项】
- java后台服务器向手机端返回Json, 使用 "net.sf.json.JSONObject" 解析出现的异常问题
- « AFNetworking 图片的本地缓存问题 Get application bundle seed ID in iOS » AFNetworking 使用总结 (用法+JSON解析)
- Word退出时报错 "此文件正由另一个应用程序或用户使用"
- 网页速度分析 && 网页头文件解析 && 性能优化 && httpwatch工具使用
- 在.jnlp文件中使用"all-permissions"标签的解决方案
- 使用<input type="file">实现文件上传
- 导入js文件时,不使用".."或"~"
- Word退出时报错 "此文件正由另一个应用程序或用户使用"
- win 7 删除了用户文件夹后出现的&quot;你已使用临时配置文件登录&quot;提示的解决方法
- C++中头文件使用""与<>的区别
- vc6.0无法使用“添加到工程”或者"打开文件"解决方法
- dired显示文件大小时使用"human readable"的格式显示
- "libxml/parser.h: 没有那个文件或目录"解决方案
- 在DP中使用"滚动数组"
- swift:使用第三方开源库SwiftyJSON解析本地json数据文件
- Newtonsoft.Json.dll解析json的dll文件使用
- 使用truss、strace或ltrace诊断软件的"疑难杂症"