码迷,mamicode.com
首页 > Web开发 > 详细

系统自带的NSJSONSerialization解析json文件

时间:2016-05-10 18:24:05      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

 1 #import "ViewController.h"
 2 #import "Student.h"
 3 #import "GDataXMLNode.h"
 4 #import "JSONKit.h"
 5 
 6 @interface ViewController () <NSXMLParserDelegate>
 7 
 8 /**
 9  *  存储数据的数组
10  */
11 @property (nonatomic, strong) NSMutableArray *dataArray;
12 
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     // Do any additional setup after loading the view, typically from a nib.
20 }
21 
22 #pragma mark - 系统自带的json数据解析
23 - (IBAction)foundationParserActionJSONDocument:(UIButton *)sender {
24     
25     // 1.获取文件路径
26     NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfo_json.txt" ofType:nil];
27     
28     
29     // 2.根据路径获取NSData
30     NSData *data = [NSData dataWithContentsOfFile:path];
31 
32     
33     // 3.对存储数据的数组进行初始化
34     self.dataArray = [NSMutableArray array];
35     
36     
37     // 4.开始进行解析
38     NSArray *resultArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
39     
40     
41     // 5.遍历数组,使用KVC给对象赋值
42     for (NSDictionary *dict in resultArray) {
43         
44         Student *stu = [[Student alloc] init];
45         
46         // 将数组中的值赋给对象
47         [stu setValuesForKeysWithDictionary:dict];
48         
49         // 将对象添加到数组中
50         [self.dataArray addObject:stu];
51     }
52     
53     
54     // 遍历检验
55     for (Student *stu in self.dataArray) {
56         NSLog(@"name = %@, gender = %@, age = %ld, hobby = %@", stu.name, stu.gender, stu.age, stu.hobby);
57     }
58     
59 }
60 
61 @end

 

系统自带的NSJSONSerialization解析json文件

标签:

原文地址:http://www.cnblogs.com/zhizunbao/p/5478359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!