JSONModel解析Dictionary To Model /JSON To Model
2024-09-05 19:27:29
你在把字典转成object的时候还在按下面这样:
self.id = [jsonDict objectForKey:@"id"];
self.name = [jsonDict objectForKey:@"name"];
self.profileImageBig = [jsonDict objectForKey:@"profile_image_big"];
self.profileImageSmall = [jsonDict objectForKey:@"profile_image_small"];
self.profileImageSquare = [jsonDict objectForKey:@"profile_image_square"];
self.firstName = [jsonDict objectForKey:@"firstName"];
self.familyName = [jsonDict objectForKey:@"familyName"];
self.age = [jsonDict objectForKey:@"age"];
这样做你就out了,太繁琐,得判断值的nil,null,类型等。使用JSONModel这样即可搞定
@interface MyModel: JSONModel
@property (strong, nonatomic) NSString* id;
@property (strong, nonatomic) NSString* name;
(etc...)
@end
添加JSONModel
pod管理的直接pod 'JSONModel'
其它的直接去下载包:https://github.com/icanzilb/JSONModel
教程参考:
http://www.touch-code-magazine.com/JSONModel/
http://www.raywenderlich.com/12139/introduction-to-cocoapods
简单介绍几个方面
指定一个索引,例如:
@property (strong, nonatomic) NSString<index>* name
你看到有一个Index,作用就是可以直接在数组中查找符合条件的对象,例如查找数组中object的name是sharofat的对象可以像下面这样写:
//查找index为sharofat的
NSArray *loans = feed.loans;
NSLog(@"modelWithIndexValue --->%@",[loans modelWithIndexValue:@"Sharofat"]);
object的数组和dict的数组相互转换,objce转json,dict
//将model的array转成dict的array
NSMutableArray *dictArray = [LoanModel arrayOfDictionariesFromModels:feed.loans];
NSLog(@"arrayOfDictionariesFromModels===>%@",dictArray); //将dict的array转成model的array
NSMutableArray *modelArray = [LoanModel arrayOfModelsFromDictionaries:dictArray];
NSLog(@"arrayOfModelsFromDictionaries===>%@",modelArray); LoanModel* loan = feed.loans[indexPath.row];
NSLog(@"loan.toDictionary--->%@",loan.toDictionary);
NSLog(@"loan.toJSONString--->%@",loan.toJSONString);
json,dict转object时判断value
-(BOOL)validate:(NSError**)err
{
if ([self.name isEqual:@"Winfred"]) {
self.name = @"Winfred rewrite name";
// return NO;
}
NSLog(@"Loan of %@", self.name);
NSLog(@"sector of %@", self.modelSector);
NSLog(@"plandate of %@", self.plandate);
return YES;
}
很重要的keyMapper,指定映射值,如果不指定就是默认的
转换带下划线的,例如:user_name 转换对应的key就是userName
+(JSONKeyMapper*)keyMapper
{
return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
}自定义key,例如: planned_expiration_date转换想对应plandate
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithJSONToModelBlock:^NSString *(NSString *keyName) { if ([keyName isEqual:@"planned_expiration_date"]) {
return @"plandate";
}else if ([keyName isEqual:@"sector"]) {
return @"modelSector";
}
else {
return keyName;
} } modelToJSONBlock:^NSString *(NSString *keyName) { if ([keyName isEqual:@"plandate"]) {
return @"planned_expiration_date";
}else if ([keyName isEqual:@"modelSector"]) {
return @"sector";
}else {
return keyName;
} }];
}
也可以像下面这样写:
+(JSONKeyMapper*)keyMapper {
return [[JSONKeyMapper alloc]initWithDictionary:@{@"sector":@"modelSector"}];
}
指定定义的key的类型
optional表示字段可选,例如
//链接字段是可选的,转换的时候允许link为空
@property (nonatomic,strong) NSString<optional> *link;
index表示索引,参照1
convertondemand转换对象数组,例如:
//表示数组是LoanModel对象
@property (strong, nonatomic) NSArray<loanmodel , ConvertOnDemand>* loans;
最新文章
- CF219D. Choosing Capital for Treeland [树形DP]
- 95、Jenkins部署.net持续集成自动化测试环境
- json处理总结(前端js和后端java)
- JSP 和 Servlet 有哪些相同点和不同点,他们之间的联系是什么?
- 移动端web之像素基础
- Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
- ASP.NET的分页方法(四)
- PHP中使用Luhn算法校验信用卡及借记卡卡号
- unidac连接FireBird数据库
- 【Hibernate步步为营】--映射合集汇总
- Java 多态,重载,重写
- 基于Angularjs实现图片上传和下载
- springmvc注解
- 了解git的命令行使用
- Visual Studio Code-批量在文末添加文本字段
- 一篇读懂HTTPS:加密原理、安全逻辑、数字证书等
- 初识Haskell
- Conversation function
- DBC格式解析(数据部分)
- Java复习总结——数据类型