美文网首页
三方框架JsonModel

三方框架JsonModel

作者: 云舒卷_js | 来源:发表于2018-05-22 19:00 被阅读0次

有关jsonModel的使用

  • 对从服务器获取到的数据data进行相应的处理,转换model方式如下:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];

        DictModel*model = [[DictModelalloc]initWithDictionary:dicterror:nil];

        eventModel*event = model.event_bindings[0];

        NSLog(@"mode === %@ \n\n\n event === %@",model,event)
  • key值的映射
+ (JSONKeyMapper *)keyMapper{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"events":@"automatic_events"}];
}

全局映射

//[JSONModel setGlobalKeyMapper:[
//                               [JSONKeyMapper alloc] initWithDictionary:@{
//                                                                          @"item_id":@"ID",
//                                                                          @"item.name": @"itemName"
//                                                                          }]
// ];
  • 可选属性
+ (BOOL)propertyIsIgnored:(NSString *)propertyName{
    return YES;
}
  • json校验
- (BOOL)validate:(NSError *__autoreleasing *)error{
    
    if (self.event_bindings.count <=0 ) {
        // 合法
        return NO;
    }
    return YES;
}
  • 转换成字典或者json格式的字符串
        NSDictionary *dic = [model toDictionary];
        NSString *json = [model toJSONString];

相关文章

网友评论

      本文标题:三方框架JsonModel

      本文链接:https://www.haomeiwen.com/subject/ikcydftx.html