此心安处 便是吾乡
前言
之前一直用的是YYmodel比较多,对于MJExtension里面的方法不熟悉,记录学习下
1.JSON -> Model【字典转模型】
@interface User : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) int age;
@property (strong, nonatomic) NSNumber *money;
@end
NSDictionary *dict = @{ @"name" : @"Jack", @"age" : @20, @"money" : @100.9 };
// JSON -> User
User *user = [User mj_objectWithKeyValues:dict];
NSLog(@"name=%@, age=%zd, money=%@, ", user.name, user.age, user.money);
//输出: name=Jack, age=20, money=100.9
2.JSONString -> Model【JSON字符串转模型】
// 1.定义个jsonString
NSString *jsonString = @"{\"name\":\"Tom\", \"age\":20}";
// 2.JSONString -> Model
User *user = [User mj_objectWithKeyValues:jsonString];
// 3.打印
NSLog(@"name=%@, age=%d", user.name, user.age);
// 输出:name=Tom,age=20
源码针对mj_objectWithKeyValues方法的注释解析
keyValues 字典(可以是NSDictionary、NSData、NSString)如果是嵌套的类型也是用改方法,嵌套的模型也是字典(如果模型中包含数组需要额外处理下面在讲解)
#pragma mark - 字典转模型
/**
* 通过字典来创建一个模型
* @param keyValues 字典(可以是NSDictionary、NSData、NSString)
* @return 新建的对象
*/+ (instancetype)mj_objectWithKeyValues:(id)keyValues;
如果是多层嵌套的数据结构比如下面最外层是个字典,字典里面嵌套字典以及包含数组数据结构如下图
定义的json数据可以自己验证
NSString *jsonStr = {"message":"dd","userInfo":{"name":"哈哈大笑","avatar":"http:baidu.com","likePerson":[{"avatar":"http:baidu.com","userId":1},{"avatar":"http:baidu.com","userId":2}],"bannerData":[{"avatar":"http:baidu.com","userId":3},{"avatar":"http:baidu.com","userId":2}]},"successful":true};//在.h文件声明的Model属性
@interface BannerData :NSObject
@property (nonatomic ,copy) NSString *avatar;
@property (nonatomic ,assign) NSInteger userId;
@end
@interface LikePerson :NSObject
@property (nonatomic ,copy) NSString *avatar;
@property (nonatomic ,assign) NSInteger userId;
@end
@interface UserInfo :NSObject
@property (nonatomic ,copy) NSArray<BannerData *> *bannerData;
@property (nonatomic ,copy) NSArray<LikePerson *> *likePerson;
@property (nonatomic ,copy) NSString *name;
@property (nonatomic ,copy) NSString *avatar;
@end
@interface MianModel :NSObject
@property (nonatomic ,copy) NSString *message;
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,assign) BOOL successful;
@end//.m 里面的写法
@implementation BannerData
@end
@implementation LikePerson
@end
@implementation UserInfo
//mj 解析
+ (NSDictionary *)mj_objectClassInArray {
return @{
@"bannerData":[BannerData class],@"likePerson":[LikePerson class]};
//@"bannerData":@"BannerData",@"likePerson":@"LikePerson"};一样的意思
}
@end
@implementation MianModel
@end
/**
*方法:+ (NSDictionary *)mj_objectClassInArray;数组中需要转换的模型类,
@return 字典中的key是数组属性名,value是数组中存放模型的Class(Class类型或者NSString类型)
*/
//实现方法,最外层如果是字典,只需要一一对应就好,转为model的方法是一样的,如果最外层是数组结构,那就需要第3个方式了
MianModel *mainModel = [MianModel mj_objectWithKeyValues:jsonStr];
3.JSON array -> model array【将一个字典数组转成模型数组】
NSArray *dictArray = @[ @{ @"name" : @"Jack", @"icon" : @"lufy.png" }, @{ @"name" : @"Rose", @"icon" : @"nami.png" } ];
// JSON array -> User array
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
// 打印
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png // name=Rose, icon=nami.png
//注意如果这个数组的键对应的还有个是数组对象,那么在声明的.m 里面在对应的上一层的对象里面也要写mj_objectClassInArray这个方法,方法里面写的内容,可以参考第三个的操作
#pragma mark - 字典数组转模型数组
/**
* 通过字典数组来创建一个模型数组
* @param keyValuesArray 字典数组(可以是NSDictionary、NSData、NSString)
* @return 模型数组
*/
+ (NSMutableArray *)mj_objectArrayWithKeyValuesArray:(id)keyValuesArray;
4.Model -> JSON【将一个模型转成字典】
//New model
User *user = [[User alloc] init];
user.name =@"Jack";
user.icon =@"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"Nice mood!";
//Status -> JSON
NSDictionary*statusDict = status.mj_keyValues;
NSLog(@"%@", statusDict);
/* { text = "Nice mood!"; user = { icon = "lufy.png"; name = Jack; }; }*/
//More complex situation
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 =@"a red bag";
bag.price = 205;
stu.bag = bag;
NSDictionary *stuDict = stu.mj_keyValues;
NSLog(@"%@", stuDict);
/*{ ID = 123; bag = { name = "\U5c0f\U4e66\U5305"; price = 205; }; desc = handsome; nameChangedTime = "2018-09-08"; nowName = jack; oldName = rose;}*/
5.Model array -> JSON array【将一个模型数组转成字典数组】
userArray:这是个模型数组
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
/**
* 通过模型数组来创建一个字典数组
* @param objectArray 模型数组
* @return 字典数组
*/
总结:
常用的一些方法都是这么多了吧,接下来把方法的的意思整理在一块方面后面查阅,
【更多用法】查看NSObject+MJKeyValue.h NSObject+MJCoding.h文件
MJCoding里面的用法后面再研究研究
// 字典转模型 mj_objectWithKeyValues
User *user = [User mj_objectWithKeyValues:dict];
//数组转模型 mj_objectArrayWithKeyValuesArray
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
//模型转字典 mj_keyValues
NSDictionary *stuDict = stuModel.mj_keyValues;
//模型数组转数组 mj_keyValuesArrayWithObjectArray
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
// 转为JSON Data
- (NSData *)mj_JSONData;
//转为字典或者数组
- (id)mj_JSONObject; // NSDictionary *giftDic = [jsonString mj_JSONObject];
//转为JSON 字符串
- (NSString *)mj_JSONString;/ /NSString *jsonStr = [dictArray mj_JSONString];
网友评论