iOS解析从接口返回给的json,来到oc中将这些数据转换成对应模型相应的属性,进而在需要赋值、修改、添加的地方进行利用。那么json如何快速有效的转换成模型属性呢?这里介绍给大家一个好用的第三方MJ-Extension,通过这个第三方我们就不用管键值对找不到就报错等类似的情况了。
废话不多,端上代码:
.h中
。。。
@property(nonatomic,strong)NSArray*financialSchoolInfo;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, copy) NSString *failReason;
@property (nonatomic, copy) NSString *amount;
@property(nonatomic,copy)NSString*orderExpectIncome;
@property(nonatomic,strong)CustomerPartnerInfo*customerPartnerInfo;
@property (nonatomic, strong) CustomerBasic *customerBasic;
。。。
.m中
+ (void)load{
[RequestBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return@{
@"returns" : @"returns.customerId",
@"registers" : @"returns.register",
@"source" : @"returns.source",
@"toKen" : @"returns.toKen",
@"passWord" : @"returns.passWord",
@"cityList" : @"returns.cityList",
@"verificationCode" : @"returns.verificationCode",
@"financialSchoolInfo" : @"returns.financialSchool",
@"lastLoginIp" : @"returns.lastLoginIp",
@"customerID" : @"returns.customerId",
@"isAuth" : @"returns.isAuth",
@"idAuth" : @"returns.idAuth",
@"customerToken" : @"returns.customerToken",
@"userName" : @"returns.userName",
@"customerBindcard" : @"returns.customerBindcard",
@"name" : @"returns.name",
@"inviteCode" : @"returns.inviteCode",
@"status" : @"returns.status",
@"failReason": @"returns.failReason",
@"list" : @"returns.list",
@"amount" : @"returns.amount",
@"orderExpectIncome" : @"returns.orderExpectIncome",
@"treasureProductInfos" : @"returns.treasureProductInfos",
@"productInfoRecommend" : @"returns.productInfoRecommend",
@"data" : @"returns.data",
@"cardList" : @"returns.cardList",
@"customerPartnerInfo" : @"returns.customerPartnerInfo",
@"customerBasic" : @"returns.customerBasic",
@"idNo" : @"returns.idNo",
@"validDate" : @"returns.validDate",
@"code" : @"returns.code",
@"cordNo" : @"returns.cordNo",
@"reservedMobile" : @"returns.reservedMobile",
@"ticket" : @"returns.ticket",
@"bankName" : @"returns.bankName",
@"bankCode" : @"returns.bankCode",
@"onceLimit" : @"returns.onceLimit",
@"dayLimit" : @"returns.dayLimit",
@"customerPartnerDto" : @"returns.customerPartnerDto",
@"iconUrl" : @"returns.iconUrl",
@"linkUrl" : @"returns.linkUrl",
@"shareInfo" : @"returns.shareInfo",
@"shareTitle" : @"returns.shareTitle",
@"commissionTotal" : @"returns.commissionTotal",
@"count" : @"returns.count",
@"customerBasics" : @"returns.customerBasics",
@"cashOrderDtos" : @"returns.cashOrderDtos",
@"payPassWord" : @"returns.payPassWord",
@"partnerCustomerId" : @"returns.partnerCustomerId",
@"isIdAuth" : @"returns.isIdAuth",
@"imageUrl" : @"returns.imageUrl",
@"proVersion" : @"returns.proVersion",
@"systemUrl" : @"returns.systemUrl",
@"messages" : @"returns.messages",
@"cashOrder" : @"returns.cashOrder",
@"commissionCustomerDto" : @"returns.commissionCustomerDto",
@"commissionOrderDto" : @"returns.commissionOrderDto",
@"flag" : @"returns.flag"
};
}];
[RequestBaseModel mj_setupObjectClassInArray:^NSDictionary *{
return@{
@"financialSchoolInfo" : @"FinancialSchoolInfoModel",
@"list" : @"FinancialListModel",
@"treasureProductInfos" : @"TreasureProductInfoModel",
@"productInfoRecommend" : @"TreasureProductInfoModel",
@"data" : @"BankCardModel",
@"cardList" : @"BankCardModel",
@"cityList" : @"CityListModel",
@"customerPartnerDto" : @"CustomerPartnerDtoModel",
@"customerBasics" : @"CustomerBasic",
@"customerBindcard" : @"CustomerBindcardModel",
@"cashOrderDtos" : @"CashOrderDtosModel",
@"messages" : @"MessageModel"
};
}];
}
其中.m中load中第一个子方法是将接口上的字段按照匹配到.h上面的键值对。第二个方法是将.h上数组类型包含的元素给绑定对应的模型类别
网友评论