先将第三方文件拖进工程

JSONKit.h和JSONKit.m

然后设置在ARC工程中添加MRC文件,如下图所示

JSONKit解析json数据-LMLPHP

 #import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
#import "JSONKit.h" @interface ViewController () <NSXMLParserDelegate> /**
* 存储数据的数组
*/
@property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} #pragma mark - 使用JSONKit解析json数据
- (IBAction)jsonkitParserActionJSONDocument:(UIButton *)sender { // 1.获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfo_json.txt" ofType:nil]; // 2.根据路径获取NSData
NSData *data = [NSData dataWithContentsOfFile:path]; // 3.对存储数据的数组进行初始化
self.dataArray = [NSMutableArray array]; // 4.开始进行解析
NSArray *resultArray = [data objectFromJSONData]; // 5.遍历数组,取出其中的字典,然后使用KVC给对象赋值
for (NSDictionary *dict in resultArray) { Student *stu = [[Student alloc] init]; // 将数组中的值赋给对象
[stu setValuesForKeysWithDictionary:dict]; // 将对象添加到数组中
[self.dataArray addObject:stu];
} // 遍历检验
for (Student *stu in self.dataArray) {
NSLog(@"name = %@, gender = %@, age = %ld, hobby = %@", stu.name, stu.gender, stu.age, stu.hobby);
} } @end
05-02 02:06