JSONModel解析Dictionary To Model /JSON To Model
你在把字典转成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;
JSONModel解析Dictionary To Model /JSON To Model的更多相关文章
- 【疯狂造轮子-iOS】JSON转Model系列之一
[疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...
- Swift实现JSON转Model - HandyJSON使用讲解
背景: 很多时候,我们从服务端请求下的数据都是Json格式,我们需要拿这些数据显示到我们的UI界面. 因此,我们的做法基本都会先将json转为方便使用的数据模型,或者也可以直接转字典解决. 在OC中, ...
- 【疯狂造轮子-iOS】JSON转Model系列之二
[疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...
- Codable实现json转Model,是时候干掉HandyJSON了!
自从开始使用Swift做项目,一直都在使用HandyJSON,不可否认,HandyJSON在Swift4.0是个好东西,也尝试过其它json转mode的工具,最终发现还是HandyJSON最好用. 去 ...
- C# json转model 以及model转json
1.json转model TestModel tm = new TestModel(); JavaScriptSerializer js = new JavaScriptSerializer();tm ...
- Flutter json 2 model with Built Value
Flutter json 2 model with Built Value Flutter中json转换model, 除了手动转之外, 就是利用第三方库做一些代码生成. 流行的库有: json_ser ...
- 使用GSON和泛型解析约定格式的JSON串(转)
时间紧张,先记一笔,后续优化与完善. 解决的问题: 使用GSON和泛型解析约定格式的JSON串. 背景介绍: 1.使用GSON来进行JSON串与java代码的互相转换. 2.JSON的格式如下三种: ...
- 控制层解析post请求中json数据的时候,有些属性值为空
原因: 1.默认json数据解析的时候,值会赋给键的首字母是小写的封装的bean中的属性,如果没有首字母小写的属性,也不会报错.即bean中有getXXX方法时,从json到model会增加xxx属性 ...
- MVC中@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName的区别
MVC中,在Controllers查询到数据,返回一个实体给View并显示,可以用@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName ...
随机推荐
- loj2291 「THUSC 2016」补退选
ref pkusc 快到了,做点 thusc 的题涨涨 rp-- #include <iostream> #include <cstring> #include <cst ...
- 我给女朋友讲编程总结建议篇,怎么学习html和css
总共写了11篇博客了,7篇讲html的,4篇讲网络的.不敢说写的多么好吧,最起码的是我迈出了写作的第一步,写作的过程中了解了一些其他的知识,比如SEO.几种重定向等,由于个人能力和见识有限,写出来的东 ...
- Spring框架配置beans.xml扩展
Spring学习笔记(二) 续Spring 学习笔记(一)之后,对Spring框架XML的操作进行整理 1 什么是IOC(DI) IOC = inversion of control 控制反转 D ...
- IO Streams:数据流
数据流支持原始数据类型值(布尔型,字符型,字节型,短型,长整型,浮点型和双倍型)的二进制I / O以及字符串值.所有数据流都实现了DataInput接口或DataOutput接口.本节重点介绍这些接口 ...
- 【转】Unity3D研究院之DontDestroyOnLoad的坑
http://www.xuanyusong.com/archives/2938 Unity中的一个方法DontDestroyOnLoad可以让某些游戏对象在切换场景的时候不是施放,听起来是一个非常好的 ...
- java 数据库连接 驱动相关参数
mysql: driverClass=com.mysql.jdbc.Driver connectionURL=jdbc:mysql://localhost:3306/shiro userId=root ...
- Codeforces Round #364 (Div. 2) B 标记
B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- source ~/.bashrc 什么意思
source:使当前shell读入路径为filepath的shell文件并依次执行文件中的所有语句,通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录 https://www.cn ...
- mysql source、mysqldump 导入导出数据(转)
解决了mysql gbk编码的导入导出问题,感谢作者. 一.导入数据 1.确定 数据库默认编码,比如编码 为gbk,将读入途径编码同样设为gbk,命令为: set names gb ...
- 股票交易(bzoj 1855)
Description 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价 ...