MJExtension是很多人都在用的,主要是在后台返回的JSON中做模型转换。
常规应用很简单:
Person *item = [Person mj_objectWithKeyValues:obj];//这样就可以把后台JSON数据解析到模型。
上述例子直接使用的前提是,Person中的属性,与后台返回的Key完全对应。
特殊情况:
- 如果后台返回一个id, 我们能给SAJPlantsItem定义一个id属性吗?这是不能的,NSObject中已经有一个id属性,继承于NSObject的子类当然也有id属性,此时,要怎么处理呢?
// Person.m
- (NSDictionary *)mj_replacedKeyFromPropertyName{
return @{
// 模型属性: JSON key, MJExtension 会自动将 JSON 的 key 替换为你模型中需要的属性
@"ID":@"id",
@"descrip":@"description",
@"address":@"address.province"
};
}//在mj_replacedKeyFromPropertyName中关联, 你只需要将 key 用.连接起来就行了
- 如果Person类属性
@property (copy, nonatomic) NSString *dogName;//狗名,
但是返回的数据是这样;
{
"name":"xiaofei",
"address":{"province":@"chaohu",
"school":"sitanfu",
"family":["mm",
"dd",
{"dog":"wangcai"}
],
},
}
为了获取dogName, 难不成再加个模型?
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
return @{
// 模型属性: JSON key, MJExtension 会自动将 JSON 的 key 替换为你模型中需要的属性
@"ID":@"id",
@"descrip":@"description",
// @"address":@"address.province",
@"dogName":@"address.family[2].dog",
};
}//字典就用.号, 数组就用[], 那么嵌套再深, 你也能拿到最底层的数据
解析模型成字典后,就是以下类型:
NSDictionary *dict = @{
@"name":@"xiaofei",
@"address":@{ @"province":@"chaohu",
@"school":@"sitanfu",
@"family":@[@"mm", @"dd", @{@"dog":@"wangcai"}],
},
};
Person *pp = [Person mj_objectWithKeyValues:dict];
NSLog(@"---%@----%@---",pp.name,pp.dogName);
控制台打印结果如下:
2019-07-01 13:48:14.024376+0800 mj_refresh[42046:269593] ---xiaofei----wangcai---
3 . 如果公司一定要让你用驼峰命名, 但服务器返回的数据又都是下划线, 你该怎么办?冷汗。。。。。
这里要用到 + mj_replacedKeyFromPropertyName121这个方法, 121, 代表 oneToOne...
NSDictionary *dict2 = @{
@"name_key":@"xiaoming",
@"age_key":@20,
@"info_key":@"handsome",
};
// person.h
@property(nonatomic, strong) NSString * nameKey;
@property(nonatomic, strong) NSString * infoKey;
@property(nonatomic, assign) NSInteger ageKey;
// person.m
+ (NSString *)mj_replacedKeyFromPropertyName121:(NSString *)propertyName{
return [propertyName mj_underlineFromCamel];
}
4 . 当字典中又包含字典数组时, 模型嵌套, 你要告诉 MJExtension 嵌套模型的类名
//Person.h
@property(nonatomic, strong) NSString * nameKey;
@property(nonatomic, strong) NSString * infoKey;
@property(nonatomic, assign) NSInteger ageKey;
@property(nonatomic, strong) NSArray * users;
// person.m
+ (NSDictionary *)mj_objectClassInArray{
return @{
@"users":@"User",// 或者
// @"users":[User class],
};
}
@interface User : NSObject
@property (copy, nonatomic) NSString *name;
@end
//ViewController.m
NSDictionary *dict2 = @{
@"name_key":@"xiaoming",
@"age_key":@20,
@"info_key":@"handsome",
@"users": @[@{@"name":@"xx"}, @{@"name":@"yy"}, @{@"name":@"zz"}],
};
Person *pp = [Person mj_objectWithKeyValues:dict2];
NSLog(@"---%@---",pp.mj_keyValues);
[pp.users enumerateObjectsUsingBlock:^(User * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"----%@---",obj.name);
}];
//控制台打印结果:
2019-07-01 14:07:05.315098+0800 mj[43013:285452] ---{
"age_key" = 20;
"info_key" = handsome;
"name_key" = xiaoming;
users = (
{
name = xx;
},
{
name = yy;
},
{
name = zz;
}
);
}---
2019-07-01 14:07:05.315357+0800 mj_refresh[43013:285452] ----xx---
2019-07-01 14:07:05.315545+0800 mj_refresh[43013:285452] ----yy---
2019-07-01 14:07:05.315681+0800 mj_refresh[43013:285452] ----zz---
转换方式,用Block形式:
[Person mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{
// 模型属性: JSON key, MJExtension 会自动将 JSON 的 key 替换为你模型中需要的属性
@"ID":@"id",
@"descrip":@"description",
// @"address":@"address.province",
@"dogName":@"address.family[2].dog",
};
}];
[Person mj_setupReplacedKeyFromPropertyName121:^NSString *(NSString *propertyName) {
return [propertyName mj_underlineFromCamel];
}];
[Person mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"users":@"User",// 或者
// @"users":[User class],
};
}];
太繁杂了,有木有,全都在Person类中,能否单独抽取,能!
放到load即可。
// MJExtensionModelConfig.m
#import "MJExtensionModelConfig.h"
#import "Person.h"
#import "MJExtension.h"
//#import "User.h"
@implementation MJExtensionModelConfig
+ (void)load
{
// 例子 2.
[Person mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{
// 模型属性: JSON key, MJExtension 会自动将 JSON 的 key 替换为你模型中需要的属性
@"ID":@"id",
@"descrip":@"description",
// @"address":@"address.province",
@"dogName":@"address.family[2].dog",
};
}];
// 例子 3.
[Person mj_setupReplacedKeyFromPropertyName121:^NSString *(NSString *propertyName) {
return [propertyName mj_underlineFromCamel];
}];
// 例子 4.
[Person mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"users":@"User",// 或者
//@"users":[User class],
};
}];
}