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
时间: 2024-10-05 13:59:06