• 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];
        }
    }
  • 相关阅读:
    9.10 作业
    Day 03 作业
    Day02作业
    Day09 函数
    day08 简单习题
    Day04 python数据类型和词云的生成
    JAVA: 子类通过static块“覆盖”父类的成员变量风险
    JAVA: 子类“覆盖”父类的成员变量
    Java 访问控制权限
    Java数组类型转为集合类型
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5273189.html
Copyright © 2020-2023  润新知