码迷,mamicode.com
首页 > Web开发 > 详细

JSON 数据的系统解析

时间:2015-07-20 19:37:50      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:数据   json数据解析   

- (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];
    }
}

版权声明:本文为outlan原创文章,未经博主允许不得转载。

JSON 数据的系统解析

标签:数据   json数据解析   

原文地址:http://blog.csdn.net/yadong_zhao/article/details/46970829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!