系统自带的NSJSONSerialization解析json文件

 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

 

转载于:https://www.cnblogs.com/zhizunbao/p/5478359.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值