MJExtension是一套字典和模型之間互相轉(zhuǎn)換的超輕量級框架
MJExtension能完成的功能
字典(JSON) --> 模型(Model)
模型(Model) --> 字典(JSON)
字典數(shù)組(JSON Array) --> 模型數(shù)組(Model Array)
模型數(shù)組(Model Array) --> 字典數(shù)組(JSON Array)
JSON array -> model array【字典數(shù)組轉(zhuǎn)成模型數(shù)組】
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
];
// JSON array -> User array
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
// Printing
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png
Model -> JSON【模型轉(zhuǎn)成字典】
// 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;
}
*/
常見用法
- key替換蛇捌,比如ID和id
1.在模型類.m文件引入"MJExtension.h"
2.實現(xiàn)方法
+ (NSDictionary *)replacedKeyFromPropertyName {
return @{@“非關(guān)鍵字的屬性名” : @“數(shù)組的key”};
}
+ (NSDictionary *)replacedKeyFromPropertyName{ // 模型的desc屬性對應(yīng)著字典中的description
return @{@"desc" : @"description",@"ID" : @"id"};
}
方法將model文件中定義的字段名轉(zhuǎn)化為與請求數(shù)據(jù)中相同的,使兩者的內(nèi)部相同渴频,只是名稱不同
通過遇到有的字段是一個數(shù)組叫挟,數(shù)組中又存放了其他的數(shù)據(jù)模型對象刽沾,可以通過以下方法將其關(guān)聯(lián)
+(NSDictionary *)objectClassInArray{
return @{@"businesses" : [Busnisses class]};
}
實例一<模型中還有模型數(shù)組>
.h文件
#import <Foundation/Foundation.h>
#import "HWFindHeaderseItem.h"
@interface HWFindResponseItem : NSObject
@property(nonatomic, strong) HWFindHeaderseItem *header; // 直接是字典 直接定義要轉(zhuǎn)的模型類型
@property(nonatomic, strong) NSArray *chics; // 是模型數(shù)組 需要在.m文件中重寫objectClassInArray方法
@property(nonatomic, strong) NSArray *featured;
@end
.m文件
#import "HWFindResponseItem.h"
#import <MJExtension/MJExtension.h>
#import "HWFindChicsItem.h"
#import "HWFindFeaturesItem.h"
@implementation HWFindResponseItem
+ (NSDictionary *)objectClassInArray{
return @{@"chics" : [HWFindChicsItem class], @"featured" : [HWFindFeaturesItem class]};
}
@end
實例二<模型中屬性與系統(tǒng)屬性同名>
.h文件
#import <Foundation/Foundation.h>
@interface HWFindHeaderseItem : NSObject
@property(nonatomic, strong) NSString *title;
@property(nonatomic, strong) NSString *desc; // 與系統(tǒng)屬性重名 需要在.m文件中重寫replacedKeyFromPropertyName告知
@property(nonatomic, strong) NSArray *photos;
@end
.m文件
#import "HWFindHeaderseItem.h"
#import <MJExtension/MJExtension.h>
@implementation HWFindHeaderseItem
+ (NSDictionary *)replacedKeyFromPropertyName{ // 模型的desc屬性對應(yīng)著字典中的description
return @{@"desc" : @"description"};
}
- (void)setTitle:(NSString *)title {
_title = title;
NSLog(@"%@", title);
}
@end