JSON 数据的系统解析

页面导航:首页 > 网络编程 > JavaScript > JSON 数据的系统解析

JSON 数据的系统解析

来源: 作者: 时间:2016-02-01 09:31 【

- (IBAction)jsonSystemButtonDidClicked:(UIButton *)sender { self JSONArray = [NSMutableArray array]; 1、拿到 json 文件的路径 NSString *path = [[NSBundle
- (IBAction)jsonSystemButtonDidClicked:(UIButton *)sender {

    self.JSONArray = [NSMutableArray array];

    // 1、拿到 json 文件的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@student ofType:@json];

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

    // 3、解析开始
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];


    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);
    }
}
@property (nonatomic, strong) NSMutableArray *JSONArray;

当模型中类型冲突时

@implementation StudentModal

- (NSString *)description
{
    return [NSString stringWithFormat:@%@, %@, %@, %ld, %@, self.name, self.sex,self.phoneNumber, self.number, self.hobby
            ];
}

- (void)setValue:(id)value forKey:(NSString *)key
{
    // super 必须写
    [super setValue:value forKey:key];
    if ([key isEqualToString:@number]) {
        self.number = [value integerValue];
    }
}

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<