MJExtension能做什么?
- MJExtension是一套字典和模型之间互相转换的超轻量级框架
- MJExtension能完成的功能
字典(JSON) --> 模型(Model)
模型(Model) --> 字典(JSON)
字典数组(JSON Array) --> 模型数组(Model Array)
模型数组(Model Array) --> 字典数组(JSON Array)
1.最简单的字典转模型
新建User类
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 (assign, nonatomic) Sex sex;
@end
在viewController里面
NSDictionary *dict1 = @{
@"name" : @"Ellis",
@"icon" : @"hello.png",
@"age" : @22,
@"height" : @172.0,
@"sex" : @(SexMale)
};
User *user1 = [User mj_objectWithKeyValues:dict1];
NSLog(@"name=%@, icon=%@, age=%d, height=%f, sex=%d", user1.name, user1.icon, user1.age, user1.height, user1.sex);
2.模型中嵌套模型
新建Status类
#import <Foundation/Foundation.h>
#import "User.h"
@interface Status : NSObject
/** 微博的文本内容 */
@property (copy, nonatomic) NSString *text;
/** 作者 */
@property (strong, nonatomic) User *user;
/** 转发的微博 */
@property (strong, nonatomic) Status *retweetedStatus;
@end
在controller里面
NSDictionary *dict2 = @{
@"text" : @"天气不错",
@"user" : @{
@"name" : @"jack",
@"icon" : @"1.png"
},
@"retweetedStatus" : @{
@"text" : @"天气是不错",
@"user" : @{
@"name" : @"rose",
@"icon" : @"2.png"
}
}
};
Status *status1 = [Status mj_objectWithKeyValues:dict2];
NSString *text = status1.text;
NSString *name = status1.user.name;
NSString *icon = status1.user.icon;
NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
NSString *text2 = status1.retweetedStatus.text;
NSString *name2 = status1.retweetedStatus.user.name;
NSString *icon2 = status1.retweetedStatus.user.icon;
NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
3.模型中有个数组属性,数组里面又要装着其它模型
新建Ad广告类 和StatusResult类
/** 广告的数据模型 */
@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 *)mj_objectClassInArray{
return @{
@"statuses" : @"Status",
@"ads" : @"Ad"
};
}
@end
controller里面
NSDictionary *dict3 = @{
@"statuses" : @[
@{
@"text" : @"天气不错",
@"user" : @{
@"name" : @"wky",
@"icon" : @"wky.png"
}
},
@{
@"text" : @"天气是不错",
@"user" : @{
@"name" : @"smy",
@"icon" : @"smy.png"
}
}
],
@"ads" : @[
@{
@"image" : @"ad1.png",
@"url" : @"www.ad1.com"
},
@{
@"image" : @"ad2.png",
@"url" : @"www.ad2.com"
}
],
@"totalNumber" : @"2016"
};
StatusResult *result = [StatusResult mj_objectWithKeyValues:dict3];
NSLog(@"totalNumber=%@", result.totalNumber);
for (Status *status in result.statuses) {
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
}
for (Ad *ad in result.ads) {
NSLog(@"image=%@, url=%@", ad.image, ad.url);
}
4.模型中的属性名和字典中的key不相同(或者需要多级映射)
新建Bag和Student类
@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 *oldName;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (strong, nonatomic) Bag *bag;
@end
@implementation Student
// 实现这个方法的目的:告诉MJExtension框架模型中的属性名对应着字典的哪个key
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info.nameChangedTime",
@"bag" : @"other.bag"
};
}
@end
controller里面
NSDictionary *dict4 = @{
@"id" : @"01",
@"desciption" : @"孩子",
@"name" : @{
@"oldName" : @"cat",
@"newName" : @"dog",
@"info" : @{
@"nameChangedTime" : @"2016-05"
}
},
@"other" : @{
@"bag" : @{
@"name" : @"书包",
@"price" : @200
}
}
};
Student *student = [Student mj_objectWithKeyValues:dict4];
NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", student.ID, student.desc, student.oldName, student.nowName, student.nameChangedTime);
NSLog(@"bagName=%@, bagPrice=%f", student.bag.name, student.bag.price);
5.将一个字典数组转换成模型数组
NSArray *dictArray = @[
@{
@"name" : @"jack",
@"icon" : @"jack.png"
},
@{
@"name" : @"rose",
@"icon" : @"rose.png"
}
];
//将字典的数组转换为User模型的数组
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
6.将一个模型转换为字典
User *user = [[User alloc] init];
user.name = @"wky";
user.icon = @"wky.png";
Status *status2 = [[Status alloc] init];
status2.user = user;
status2.text = @"我们去打球吧";
NSDictionary *statusDict = status2.mj_keyValues;
NSLog(@"%@", statusDict);
//多级映射的模型
Student *stu = [[Student alloc] init];
stu.ID = @"1";
stu.oldName = @"faker";
stu.nowName = @"wky";
stu.desc = @"handsome";
stu.nameChangedTime = @"2016-05-08";
Bag *bag = [[Bag alloc] init];
stu.bag = bag;
bag.name = @"adidas";
bag.price = 999;
NSDictionary *stuDict = stu.mj_keyValues;
NSLog(@"%@", stuDict);
7.将一个模型数组转成字典数组
User *user2 = [[User alloc] init];
user2.name = @"jack";
user2.icon = @"jack.png";
User *user3 = [[User alloc] init];
user3.name = @"rose";
user3.icon = @"rose.png";
NSArray *usersArray = @[user2, user3];
//讲数组模型转换为字典数组
NSArray *dictsArray = [User mj_keyValuesArrayWithObjectArray:usersArray];
NSLog(@"%@", dictsArray);