MJExtension的使用
第一次使用MJExtension,一开始就让我接触解析这么有难度的model,刚开始真的是不知道怎么下手,怎么构造model,请教了别人自己的model创建,解析是不是正确,感觉也都说的不是很清楚......
总结之下就是:
要学会一个框架,一定要自己动手实战,只有自己做了一遍,后面再复杂的也都能很好的解决处理了.
以下是我这次项目中用到的,在这里记录下:
Model的定义:.h文件
1 #pragma mark - typeModel 2 3 @interface typeModel:CopiableModel 4 5 @property (nonatomic , strong) NSString *value; 6 @property (nonatomic , strong) NSString *name; 7 @property (nonatomic , strong) NSString *ID; 8 @property (nonatomic , strong) NSString *sort; 9 @property (nonatomic , strong) NSString *childnum; 10 @property (nonatomic , strong) NSString *scrapyear; 11 12 @end 13 14 #pragma mark - modelModel 15 16 @interface modelModel:NSObject 17 18 @property (nonatomic , strong) NSDictionary *companyid; 19 @property (nonatomic , strong) NSString *name; 20 @property (nonatomic , strong) NSString *ID; 21 22 @end 23 24 #pragma mark - personModel 25 26 @interface personModel:NSObject 27 28 @property (nonatomic , strong) NSString *ID; 29 @property (nonatomic , strong) NSString *name; 30 @property (nonatomic , strong) NSString *sort; 31 32 @end 33 34 #pragma mark - checkModel 35 36 @interface checkModel:NSObject 37 38 @property (nonatomic , strong) NSString *ID; 39 @property (nonatomic , strong) NSString *name; 40 @property (nonatomic , strong) NSString *sort; 41 @property (nonatomic , strong) NSString *childnum; 42 43 @end 44 45 #pragma mark - applydepidModel 46 47 @interface applydepidModel : NSObject 48 /** 49 50 id Out String 51 name Out String 52 code Out String 53 childnum Out String 54 sort Out String 55 56 */ 57 @property (nonatomic , copy) NSString *ID; 58 @property (nonatomic , strong) NSString *name; 59 @property (nonatomic , copy) NSString *code; 60 @property (nonatomic , strong) NSString *childnum; 61 @property (nonatomic , copy) NSString *sort; 62 63 @end 64 65 #pragma mark - applyuseridModel 66 67 @interface applyuseridModel : NSObject 68 /** 69 id Out String id 70 name Out String 申请人姓名 71 sort Out String 排序字段 72 73 */ 74 @property (nonatomic , copy) NSString *ID; 75 @property (nonatomic , strong) NSString *name; 76 @property (nonatomic , copy) NSString *sort; 77 78 @end 79 80 #pragma mark - OrderBaseinfoModel 81 82 @interface OrderBaseinfoModel : NSObject 83 /** 84 * id Out String 单据ID 85 applydepid Out JSONArray 申请部门信息 86 applytime Out String 申请时间 87 applyuserid Out JSONArray 申请人信息 88 checkuser Out String 审核人 89 code Out String 单据编号 90 confirm Out String 用户是否确认领取 91 orgid Out String 校区id 92 remark Out 备注 93 status Out 状态代码 94 statusname Out 状态名称 95 */ 96 @property (nonatomic , copy) NSString *ID; 97 @property (nonatomic , copy) NSString *allprice; 98 @property (nonatomic , strong) NSString *applytime; 99 @property (nonatomic , strong) applydepidModel *applydepid; 100 @property (nonatomic , strong) applyuseridModel *applyuserid; 101 @property (nonatomic , strong) checkModel *checkdepid; 102 @property (nonatomic , copy) NSString *code; 103 @property (nonatomic , copy) NSString *confirm; 104 @property (nonatomic , copy) NSString *orgid; 105 @property (nonatomic , copy) NSString *remark; 106 @property (nonatomic , copy) NSString *status; 107 @property (nonatomic , copy) NSString *statusname; 108 @property (nonatomic , copy) NSString *returntime; 109 110 @end 111 112 #pragma mark - companyModel 113 114 @interface companyModel:NSObject 115 116 @property (nonatomic , strong) NSString *ID; 117 @property (nonatomic , strong) NSString *name; 118 119 @end 120 121 #pragma mark - AplassetslistModel 122 123 @interface AplassetslistModel : CopiableModel 124 125 /* 126 id Out String id 127 name Out String 资产名称 128 modelsize Out String 规格 129 num Out String 数量 130 orderid Out String 单据id 131 orgid Out String 校区id 132 remark Out String 备注 133 */ 134 135 @property (nonatomic , copy) NSString *ID; 136 @property (nonatomic , strong) companyModel *company; 137 @property (nonatomic , strong) modelModel *model; 138 @property (nonatomic , copy) NSString *name; 139 @property (nonatomic , copy) NSString *modelsize; 140 @property (nonatomic , strong) NSString *num; 141 @property (nonatomic , copy) NSString *orderid; 142 @property (nonatomic , copy) NSString *orgid; 143 @property (nonatomic , copy) NSString *remark; 144 @property (nonatomic , strong) NSString *price; 145 @property (nonatomic , strong) typeModel *typemodel; 146 @property (nonatomic , strong) NSString *rettime; 147 @property (nonatomic , strong) NSString *isstorage; 148 149 @property (nonatomic , assign) BOOL isLowType; 150 151 @end 152 153 #pragma mark - ReslistModel 154 155 @interface ReslistModel : NSObject 156 /* 157 id Out String id 158 orderid Out String 单据id 159 orgid Out String 校区id 160 date Out String 审核日期 161 desid Out String 流程描述id 162 person Out String 审核人 163 result Out String 审核结果 164 sort Out String 排序字段 165 */ 166 @property (nonatomic , copy) NSString *ID; 167 @property (nonatomic , copy) NSString *orderid; 168 @property (nonatomic , copy) NSString *orgid; 169 @property (nonatomic , copy) NSString *date; 170 @property (nonatomic , copy) NSString *desid; 171 @property (nonatomic , strong) personModel *person; 172 @property (nonatomic , copy) NSString *result; 173 @property (nonatomic , strong) NSString *sort; 174 175 @end 176 177 #pragma mark - GivenassetslistModel 178 179 @interface GivenassetslistModel : NSObject 180 /* 181 id Out String id 182 name Out String 资产名称 183 code Out String 资产编号 184 typename Out String 资产类别 185 comname Out String 厂商 186 modelname Out String 型号 187 modelsize Out String 规格 188 */ 189 @property (nonatomic , copy) NSString *ID; 190 @property (nonatomic , copy) NSString *name; 191 @property (nonatomic , copy) NSString *code; 192 @property (nonatomic , copy) NSString *Typename; 193 @property (nonatomic , copy) NSString *comname; 194 @property (nonatomic , copy) NSString *modelname; 195 @property (nonatomic , copy) NSString *modelsize; 196 197 @end 198 199 #pragma mark - DetailModel 200 201 @interface DetailModel : NSObject 202 203 @property (nonatomic , copy) NSString *code; 204 @property (nonatomic , copy) NSString *latest_version; 205 @property (nonatomic , strong) OrderBaseinfoModel *orderbaseinfo; 206 @property (nonatomic , strong) NSMutableArray *aplassetslist; 207 @property (nonatomic , strong) NSMutableArray *reslist; 208 @property (nonatomic , strong) NSMutableArray *givenassetslist;
.m文件的定义:
对需要处理的字段的定义和转化,如服务器返回的id字段
1 @implementation modelModel 2 3 + (NSDictionary *)mj_replacedKeyFromPropertyName 4 { 5 return @{ 6 @"ID" : @"id" 7 }; 8 } 9 10 @end 11 12 13 @implementation personModel 14 15 + (NSDictionary *)mj_replacedKeyFromPropertyName 16 { 17 return @{ 18 @"ID" : @"id" 19 }; 20 } 21 22 23 @end 24 25 26 @implementation companyModel 27 28 + (NSDictionary *)mj_replacedKeyFromPropertyName 29 { 30 return @{ 31 @"ID" : @"id" 32 }; 33 } 34 35 36 @end 37 @implementation DetailModel 38 39 + (NSDictionary *)mj_objectClassInArray 40 { 41 return @{ 42 @"aplassetslist" : @"AplassetslistModel", 43 @"reslist" : @"ReslistModel", 44 @"givenassetslist" : @"GivenassetslistModel", 45 }; 46 } 47 48 49 @end 50 51 @implementation OrderBaseinfoModel 52 53 + (NSDictionary *)mj_replacedKeyFromPropertyName{ 54 55 return @{@"ID" : @"id", 56 }; 57 58 59 } 60 @end 61 62 @implementation applydepidModel 63 64 + (NSDictionary *)mj_replacedKeyFromPropertyName{ 65 66 return @{@"ID" : @"id"}; 67 68 } 69 70 @end 71 72 @implementation applyuseridModel 73 74 + (NSDictionary *)mj_replacedKeyFromPropertyName{ 75 76 return @{@"ID" : @"id"}; 77 78 } 79 @end 80 81 @implementation typeModel 82 83 + (NSDictionary *)mj_replacedKeyFromPropertyName{ 84 85 return @{@"ID" : @"id"}; 86 87 } 88 @end 89 90 91 @implementation checkModel 92 93 + (NSDictionary *)mj_replacedKeyFromPropertyName{ 94 95 return @{@"ID" : @"id"}; 96 97 } 98 @end 99 100 101 @implementation AplassetslistModel 102 103 + (NSDictionary *)mj_replacedKeyFromPropertyName{ 104 105 return @{@"ID" : @"id", 106 @"typemodel" : @"type"}; 107 108 }
一句代码解决JSON转Model:
self.detailModel = [DetailModel mj_objectWithKeyValues:responseObject];
现在发现MJExtension还是特别方便的....前提是model构建好......
加油....每天学习新东西!!!!