有关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];
网友评论