一, 简介
1.MJExtension是一套字典和模型之间互相转换的超轻量级框架,支持以下类型转换:
- JSON –> Model、Core Data Model
- JSONString –> Model、Core Data Model
- Model、Core Data Model –> JSON
- JSON Array –> Model Array、Core Data Model Array
- JSONString –> Model Array、Core Data Model Array
- Model Array、Core Data Model Array –> JSON Array
- Coding all properties of model in one line code.
二, 如何使用
cocoapods导入:pod 'MJExtension'
三,常用 API 说明
示例中相关类.h
typedef enum {
SexMale,
SexFemale
} Sex;
@interface User : NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *icon;
@property (assign, nonatomic) unsigned int age;
@property (copy, nonatomic) NSString *height;
@property (strong, nonatomic) NSNumber *money;
@property (assign, nonatomic) Sex sex;
@property (assign, nonatomic, getter=isGay) BOOL gay;
@end
@interface Status : NSObject
@property (copy, nonatomic) NSString *text;
@property (strong, nonatomic) User *user;
@end
@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end
@interface StatusResult : NSObject
/** Contatins Status model */
@property (strong, nonatomic) NSMutableArray *statuses;
/** Contatins Ad model */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end
@interface Bag : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end
@interface Student : NSObject
@property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *desc;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (strong, nonatomic) Bag *bag;
@end
1.字典转模型.
NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @"1.55",
@"money" : @100.9,
@"sex" : @(SexFemale),
@"gay" : @"true"
};
//转换
User *user = [User mj_objectWithKeyValues:dict];
2.模型中嵌套模型
NSDictionary *dict = @{
@"name" : @"Agree!Nice weather!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@"retweetedStatus" : @{
@"text" : @"Nice weather!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}
};
//转换
Status *status = [Status mj_objectWithKeyValues:dict];
3.模型中有个数组属性,数组里面又要装着其他模型
// 1.需要设置数组中包含哪些model key:属性名字 value:包含的model名字
[StatusResult mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"statuses" : @"Status",
// @"statuses" : [Status class],
@"ads" : @"Ad"
// @"ads" : [Ad class]
};
}];
或者在相关类的.m文件中实现
/**
* 数组中需要转换的模型类
*
* @return 字典中的key是数组属性名,value是数组中存放模型的Class(Class类型或者NSString类型)
*/
+ (NSDictionary *)mj_replacedKeyFromPropertyName;
//2.
NSDictionary *dict = @{
@"totalNumber" : @"2014",
@"statuses" : @[
@{
@"text" : @"Nice weather!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
},
@{
@"text" : @"Go camping tomorrow!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
}
}
],
@"ads" : @[
@{
@"image" : @"ad01.png",
@"url" : @"http://www.ad01.com"
},
@{
@"image" : @"ad02.png",
@"url" : @"http://www.ad02.com"
}
]
};
//转换
StatusResult *result = [StatusResult mj_objectWithKeyValues:dict];
注意不要忘了告诉MJExtension数组中包含的模型
3. 字典数组转成模型数组
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
];
//转换
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
4.模型转字典
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"Nice mood!";
//转换
NSDictionary *statusDict = status.mj_keyValues;
5. 模型数组转字典数组
User *user1 = [[User alloc] init];
user1.name = @"Jack";
user1.icon = @"lufy.png";
User *user2 = [[User alloc] init];
user2.name = @"Rose";
user2.icon = @"nami.png";
NSArray *userArray = @[user1, user2];
//转换
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
6. 允许转换哪些属性
//在.m中实现
+ (NSArray *)mj_allowedPropertyNames;//只有这个数组中的属性名才允许进行字典和模型的转换
或者相关Model调用
+ (void)mj_setupAllowedPropertyNames:(MJAllowedPropertyNames)allowedPropertyNames;
7.忽略哪些属性转换
//在.m中实现
+ (NSArray *)mj_ignoredPropertyNames;
或者相关Model调用
+ (void)mj_setupIgnoredPropertyNames:(MJIgnoredPropertyNames)ignoredPropertyNames;
8. 将属性名换为其他key去字典中取值
//.m中实现
// 字典中的key是属性名,value是从字典中取值
+ (NSDictionary *)mj_replacedKeyFromPropertyName;
9.转换完成后的回调
// 当字典转模型完毕时调用
- (void)mj_keyValuesDidFinishConvertingToObject;
// 当模型转字典完毕时调用
- (void)mj_objectDidFinishConvertingToKeyValues;
若有需要只需在相关类中实现以上方法即可
四:注意事项
1.首先要分析需要转换的数据结构调用正确的API.
2.遇到结构比较复杂的数据后,可以考虑分层转换,不一定每一层都要按照固定的模式去转换.
3.转换嵌套结构的时候,一定要注意看是否需要实现某些方法告诉MJExtension包含哪些模型.