JSON 数据的 JSONKit 解析

来源:互联网 发布:搞笑网络歌曲排行榜 编辑:程序博客网 时间:2024/06/05 17:41
@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);}
0 0