JSON 数据的 JSONKit 解析

@property (nonatomic, strong) NSMutableArray *JSONArray;

懒加载

- (NSMutableArray *)JSONArray
{
    if (_JSONArray == nil) {
        self.JSONArray = [NSMutableArray array];
    }
    return _JSONArray;
}
- (IBAction)JSONKitButtonDidClicked:(UIButton *)sender {
    // 1、拿到 json 文件的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"json"];

    // 2、根据路径获取文件内容为 NSData 对象
    NSData *data = [NSData dataWithContentsOfFile:path];

    // 3、解析为数组
    NSArray *array = [data objectFromJSONData];


    for (NSDictionary *dict in array) {
        StudentModal *modal = [StudentModal new];
        [modal setValuesForKeysWithDictionary:dict];
        [self.JSONArray addObject:modal];
    }

    for (StudentModal *modal in self.JSONArray) {
        NSLog(@"JSONArray = %@, %ld, %@", modal.name, modal.number, modal.hobby);
    }

    [self.JSONArray removeAllObjects];

    // 把OC里面的数据类型转化成 json 串
    NSString *yadong;
    [yadong JSONString];

    NSArray *testArray = @[@"wewe", @"www", @"qq"];

    NSString *result = [testArray JSONString];
    NSLog(@"result = %@", result);

}

你可能感兴趣的:(数据,json解析,NSMutableArray)