使用"JSONKit.h"解析文件

来源:互联网 发布:14.04 linux 输入法 编辑:程序博客网 时间:2024/06/09 15:22
使用"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);
0 0
原创粉丝点击