MJExtension是一套字典和模型之间互相转换的超轻量级框架
字典(JSON) --> 模型(Model)
模型(Model) --> 字典(JSON)字典数组(JSON Array) --> 模型数组(Model Array)
模型数组(Model Array) --> 字典数组(JSON Array)
MJExtension JSONModel、Mantle区别
MJExtension > JSONModel > Mantle(转换效率)
JSONModel: 要求所有模型类必须继承自JSONModel基类
MJExtension:不需要继承任何特殊基类,毫无污染,毫无侵入性
Mantle: 要求所有模型类必须继承自MTModel基类个人感觉上MJExtension容错性更高一些
1.字典转模型
typedef enum {
SexMale,
SexFemale
} Sex;
@interface User : NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *icon;
@property (assign, nonatomic) int age;
@property (assign, nonatomic) double height;
@property (nonatomic, strong) NSString *birthday;
@property (strong, nonatomic) NSNumber *money;
@property (assign, nonatomic) Sex sex;
@end
NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @"1.55",
@"money" : @100.9,
@"sex" : @(SexFemale)
};
User *user = [User mj_objectWithKeyValues:dict];
改变映射关系
如果后台返回的字段跟模型的字段不一致,可以在.m文件中实现匹配方法
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
return @{
//前边的是你想用的key,后边的是返回的key
@"name":@"name_nick"
};
}
复杂映射更改
@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
@implementation Student
// 告诉MJExtension框架模型中的属性名对应着字典的哪个key
+ (NSDictionary *)replacedKeyFromPropertyName{
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info.nameChangedTime",
@"bag" : @"other.bag"
};
}
@end
NSDictionary *dict = @{
@"id" : @"20",
@"desciption" : @"孩子",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @{
@"nameChangedTime" : @"2013-08"
}
},
@"other" : @{
@"bag" : @{
@"name" : @"小书包",
@"price" : @100.7
}
}
};
// 将字典转为Student模型
Student *stu = [Student objectWithKeyValues:dict];
数据简单加工
后台如果返回时间戳,但我们需要指定时间格式,可以在.m 文件中实现数据处理方法
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property{
if ([property.name isEqualToString:@"birthday"]) {
if (oldValue) {
// 格式化时间
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[oldValue doubleValue]];
NSString* dateString = [formatter stringFromDate:date];
return dateString;
}
}else {
return @"日期有误";
}
return oldValue;
}
=======================================
以上转化为Model 的方法都是 mj_objectWithKeyValues
=======================================
2.模型中嵌套模型
如果全是字典就不用做任何处理
@interface Status : NSObject
@property (copy, nonatomic) NSString *text;
@property (strong, nonatomic) User *user;
@property (strong, nonatomic) Status *retweetedStatus;
@end
NSDictionary *dict = @{
@"text" : @"是啊,今天天气确实不错!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@"retweetedStatus" : @{
@"text" : @"今天天气真不错!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}
};
// 将字典转为Status模型
Status *status = [Status objectWithKeyValues:dict];
3. 模型中有数组
如果模型中有包含模型的数组,则需要实现相应方法
@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end
@interface StatusResult : NSObject
/** (里面都是Status模型) */
@property (strong, nonatomic) NSMutableArray *statuses;
/** (里面都是Ad模型) */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end
@implementation StatusResult
#第一种方法:告诉MJExtension框架statuses和ads数组里面模型
+ (NSDictionary *)objectClassInArray{
return @{
@"statuses" : [Status class],
@"ads" : [Ad class]
};
}
+ (Class)objectClassInArray:(NSString *)propertyName{
if ([propertyName isEqualToString:@"statuses"]) {
return [Status class];
} else if ([propertyName isEqualToString:@"ads"]) {
return [Ad class];
}
return nil;
}
#第二种方法对:比上面的2个方法更加没有侵入性和污染,因为不需要导入Status和Ad的头文件
+ (NSDictionary *)objectClassInArray{
return @{
#前边,是属性数组的名字,后边就是类名
@"statuses" : @"Status",
@"ads" : @"Ad"
};
}
@end
NSDictionary *dict = @{
@"statuses" : @[
@{
@"text" : @"今天天气真不错!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
},
@{
@"text" : @"明天去旅游了",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
}
}
],
@"ads" :@[
@{
@"image" : @"ad01.png",
@"url" : @"http://www.ad01.com"
},
@{
@"image" : @"ad02.png",
@"url" : @"http://www.ad02.com"
}
],
@"totalNumber" : @"2014"
};
// 将字典转为StatusResult模型
StatusResult *result = [StatusResult objectWithKeyValues:dict];
// 打印statuses数组中的模型属性
for (Status *status in result.statuses) {
NSLog(@"text=%@, name=%@", text, name);
}
// 打印ads数组中的模型属性
for (Ad *ad in result.ads) {
NSLog(@"image=%@, url=%@", ad.image, ad.url);
}
4. 字典数组转成模型数组
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png",
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png",
}
];
// 将字典数组转为User模型数组
NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray];
// 打印userArray数组中的User模型属性
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
5.模型转字典
// 新建模型
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"今天的心情不错!";
// 将模型转为字典
NSDictionary *statusDict = status.keyValues;
NSLog(@"%@", statusDict);
/*{ text = "今天的心情不错!";
user = {
icon = "lufy.png";
name = Jack;
};
}*/
// 多级映射的模型
Student *stu = [[Student alloc] init];
stu.ID = @"123";
stu.oldName = @"rose";
stu.nowName = @"jack";
stu.desc = @"handsome";
stu.nameChangedTime = @"2018-09-08";
Bag *bag = [[Bag alloc] init];
bag.name = @"小书包";
bag.price = 205;
stu.bag = bag;
NSDictionary *stuDict = stu.keyValues;
6. 模型数组转字典数组
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 keyValuesArrayWithObjectArray:userArray];