iOS Json和对象互转(反射支持对象嵌套的复杂对象类型,支持基本数据类型和NSDate属性转化)
2013-11-09 16:01
946 查看
背景:
JsonKit 接收网络数据转化成NSDictionary还算好用;
之前的项目 实体类 属性字段比较少,偷懒的做法 是 遍历 dictionary 利用objectForKey 逐个对 实体对象属性赋值;
但是现在的项目开始变态了,实体非常多,而且 实体属性字段 动辄几十个,就像下面这种类型:
作为一个正常人, 如何能忍受挨个遍历手动forkey....
如果曾经你也是一名java程序员,或者看过java代码, 你不难怀念 objectParser;传入json数据和对应class,直接给你返回要用的对象;
既然没有,那只能自己干!!!
首先要考虑的几个问题:
1.实体属性 类型的问题,特别是基本类型:double ,int, float等。
2.java中有泛型,而iOS却没有, 有什么办法将array中得对象class告知程序;
对于问题1.已经可以使用iOS特性KVC setObject forKey ,传入 string , 它(编译器)会自动转化成需要的类型, 要注意的是 long 一定得声明为long long;
对于问题2.需要后台严格遵守我们的约定:
传入的array类型的数据为key时候 ,命名统一为xxxxList如:
我在截取类名时候 拼接类名 GuCIInsureDemandResultDto 然后反射为对象,判断dto实体是否有对应key的属性, 然后利用KVC对属性赋值;
此外:此外, 有些小工具是必不可少的;将Dto实体对象 转化为JSONString 然后post给服务器。算是一个与上述功能互逆的过程;之前很傻,在每个Dto里面都写了一个toDictionary方法;然后针对 属性是否为array类型做了迭代调用,将最终的Dto转化为 NSDictionary,然后利用JSONkit转化为JSONString传给服务器;
为此,由于忍受不了每个对象这么写;大量重复工作,费时费事,我还专门写了小工具一枚,将java dto实体转化为 iosDto实体的工具;
现在想来: 直接扩展NSObject, 在里面统一处理toDictionary方法,在toDictionary里面 通过访问jbj-c/runtime获取类属性和属性列表;简单的语句就可以搞定!
至此 : iOS 实体转JSONString 和 JSONString 转实体全部搞定!
JsonKit 接收网络数据转化成NSDictionary还算好用;
之前的项目 实体类 属性字段比较少,偷懒的做法 是 遍历 dictionary 利用objectForKey 逐个对 实体对象属性赋值;
但是现在的项目开始变态了,实体非常多,而且 实体属性字段 动辄几十个,就像下面这种类型:
@interface PremiumCalculateResultDto : NSObject @property (nonatomic, retain) NSMutableArray * premiumCalculateFeeList; @property (nonatomic, retain) NSMutableArray * premiumCalculateCarShipTaxFeeList; @property (nonatomic, retain) NSMutableArray * guCIVehiclePriceResultDtoList; @property (nonatomic, retain) NSMutableArray * guCIInsureRiskItemResultDtoList; @property (nonatomic, retain) NSMutableArray * guCIInsureDemandResultDtoList;
@interface PremiumCalculateFee : NSObject @property (nonatomic, assign) double sumInsured; @property (nonatomic, assign) double grossPremium; @property (nonatomic, retain) NSString * riskCode; @property (nonatomic, retain) NSMutableArray * premiumCalculateItemKindFeeList; @property (nonatomic, assign) double benchmarkPremium; @property (nonatomic, assign) double discount;
@interface PremiumCalculateItemKindFee : NSObject @property (nonatomic, assign) double adjustRate; @property (nonatomic, assign) double rate; @property (nonatomic, assign) double netPremium; @property (nonatomic, assign) double grossPremium; @property (nonatomic, retain) NSString * riskCode; @property (nonatomic, assign) double deductible; @property (nonatomic, assign) double shortRate; @property (nonatomic, retain) NSString * kindName; @property (nonatomic, assign) double basePremium; @property (nonatomic, assign) double benchmarkPremium; @property (nonatomic, retain) NSString * kindCode; @property (nonatomic, assign) double discount;
作为一个正常人, 如何能忍受挨个遍历手动forkey....
如果曾经你也是一名java程序员,或者看过java代码, 你不难怀念 objectParser;传入json数据和对应class,直接给你返回要用的对象;
既然没有,那只能自己干!!!
首先要考虑的几个问题:
1.实体属性 类型的问题,特别是基本类型:double ,int, float等。
2.java中有泛型,而iOS却没有, 有什么办法将array中得对象class告知程序;
对于问题1.已经可以使用iOS特性KVC setObject forKey ,传入 string , 它(编译器)会自动转化成需要的类型, 要注意的是 long 一定得声明为long long;
对于问题2.需要后台严格遵守我们的约定:
传入的array类型的数据为key时候 ,命名统一为xxxxList如:
guCIInsureDemandResultDtoList = ( {
我在截取类名时候 拼接类名 GuCIInsureDemandResultDto 然后反射为对象,判断dto实体是否有对应key的属性, 然后利用KVC对属性赋值;
此外:此外, 有些小工具是必不可少的;将Dto实体对象 转化为JSONString 然后post给服务器。算是一个与上述功能互逆的过程;之前很傻,在每个Dto里面都写了一个toDictionary方法;然后针对 属性是否为array类型做了迭代调用,将最终的Dto转化为 NSDictionary,然后利用JSONkit转化为JSONString传给服务器;
为此,由于忍受不了每个对象这么写;大量重复工作,费时费事,我还专门写了小工具一枚,将java dto实体转化为 iosDto实体的工具;
现在想来: 直接扩展NSObject, 在里面统一处理toDictionary方法,在toDictionary里面 通过访问jbj-c/runtime获取类属性和属性列表;简单的语句就可以搞定!
至此 : iOS 实体转JSONString 和 JSONString 转实体全部搞定!
相关文章推荐
- String类型的json数据转化为map对象
- iOS开发-基本数据类型,对象
- OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏
- runtime 字典解析转实体model,支持基本数据类型属性,支持递归解析
- 1.js动态的往json数据添加新属性和值 2.JSON 和 JS 对象互转 3.对象转化为数组
- javascript对象转化为基本数据类型规则
- 利用反射机制动态获取对象属性名称及数据类型
- Java -- 利用反射 操作任意数组,包括对象数组 和 基本数据类型的数组
- 利用反射机制动态获取对象属性名称及数据类型
- java List Collections.sort 排序 支持 对象属性 普通数据类型 升序 降序 正序 倒序 排列
- springMVC中复杂嵌套对象、List等集合类型数据绑定
- Java -- 利用反射 操作任意数组,包括对象数组 和 基本数据类型的数组
- SharedPreferencesUtils,支持基本数据类型和任意对象
- 利用反射取得类中属性(引用及非引用数据类型)及设置该类实例化对象的属性值
- ###Springmvc封装复杂表单数据:表单提交多个实体类的参数【对象类型参数,自动封装】name属性值不能重复。和实体类属性名一致
- OC中的迭代器 值对象(把基本数据类型转化为对象类型)
- ObjC 利用反射和KVC实现嵌套对象序列化成JSON数据
- ######保存角色,add表单页面Ajax传参给action,多个集合属性,【较为复杂的页面传参(封装ajax json参数)】:注意表单数据序列化,jquery遍历,json对象转String
- javascript基础(对象,对象属性,属性基本和引用数据类型,字面量创建对象,垃圾回收,属性的枚举)(十三)
- springMVC中复杂嵌套对象、List等集合类型数据绑定