iOS Json和对象互转(反射支持对象嵌套的复杂对象类型,支持基本数据类型和NSDate属性转化)

背景:

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 转实体全部搞定!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值