节前鹏鹏让我看json反序列化为model,主要是很多场景都是从网络拿到json,然后要转成数据model,自己写的话就很费事儿,但讲真节前一天完全没心情写代码0.0 于是假期来补吧~
主要的几种方式为:KVC、Mantle、MJExtension、JSONModel、YYmodel。
以下为示例json文件~
// sample.json文件
{
"name": "ying",
"age": "25",
"school": {
"name": "SJTU",
"location": "Shanghai"
}
}
1. KVC
setValuesForKeysWithDictionary
可以直接通过dictionary把key-value设置给object,key只要和property名字一样就可以啦~
下面是model类:
#import <Foundation/Foundation.h>
#import "JsonSchool.h"
NS_ASSUME_NONNULL_BEGIN
@interface JsonPersion : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *age;
@property (nonatomic, strong) JsonSchool *school;
@end
NS_ASSUME_NONNULL_END
=======================
#import "JsonPersion.h"
@implementation JsonPersion
- (NSString *)description {
return [NSString stringWithFormat:@"name:%@, age:%@, school:%@", self.name, self.age, self.school] ;
}
@end
=======================
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface JsonSchool : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *location;
@end
NS_ASSUME_NONNULL_END
=======================
#import "JsonSchool.h"
@implementation JsonSchool
- (NSString *)description {
return [NSString stringWithFormat:@"name:%@, location:%@", self.name, self.location] ;
}
@end
解析的时只要:
#import "JsonParserViewController.h"
#import "JsonPersion.h"
@interface JsonParserViewController ()
@end
@implementation JsonParserViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *dict = [self readLocalFileWithName:@"sample"];
JsonPersion *p1 = [[JsonPersion alloc] init];
[p1 setValuesForKeysWithDictionary:dict];
NSLog(@"p1: %@", p1);
}
- (NSDictionary *)readLocalFileWithName:(NSString *)name
{
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
return [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:nil];
}
@end
输出就是酱紫的:
2020-01-25 14:24:02.915340+0800 Example1[4737:869487] p1: name:ying, age:25, school:{
location = Shanghai;
name = SJTU;
}
注意key和property必须一一对应,或者key比property少,如果dict里面的key不是property会crash的吼,而且key的名字必须和property的一致,因为其实setValuesForKeysWithDictionary就是依次调用setValueForKeyPath吧~
2. Mantle
git: https://github.com/Mantle/Mantle (使用的时候pod即可)
Mantle可以轻松把JSON数据、字典(Dictionary)和模型(即Objective对象)之间的相互转换,支持自定义映射,并且内置实现了NSCoding和NSCoping,大大简化归档操作。
对于mantle而言最重要的就是实现JSONKeyPathsByPropertyKey
,以及千万记得任何一个model类都要继承MTLModel <MTLJSONSerializing>
:
#import <Foundation/Foundation.h>
#import <Mantle/Mantle.h>
NS_ASSUME_NONNULL_BEGIN
@interface JsonSchoolMantle : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *location;
@end
@interface JsonPersonMantle : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *age;
@property (nonatomic, strong) JsonSchoolMantle *school;
@end
NS_ASSUME_NONNULL_END
=====================
#import "JsonPersonMantle.h"
@implementation JsonSchoolMantle
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name" : @"name",
@"location" : @"location"
};
}
- (NSString *)description {
return [NSString stringWithFormat:@"name:%@, location:%@", self.name, self.location] ;
}
@end
@implementation JsonPersonMantle
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
@"school":@"school"
};
}
+ (NSValueTransformer *)schoolJSONTransformer {
return [MTLJSONAdapter dictionaryTransformerWithModelClass:[JsonSchoolMantle class]];
}
- (NSString *)description {
return [NSString stringWithFormat:@"name:%@, age:%@, school:%@", self.name, self.age, self.school] ;
}
@end
使用的时候只要:
NSDictionary *dict = [self readLocalFileWithName:@"sample"];
JsonPersonMantle *p1 = [MTLJSONAdapter modelOfClass:[JsonPersonMantle class] fromJSONDictionary:dict error:nil];
NSLog(@"p1: %@", p1);
输出:
2020-01-26 09:35:13.154737+0800 Example1[6023:1175811] p1: name:ying, age:25, school:name:SJTU, location:shanghai
注意使用的时候属性名JSONTransformer
其实就是一个转换器,因为有的时候属性是NSDate这种,但是json里面就是一个string,需要转换成相应的属性类型。本例里面的school也是,由于是一个嵌套结构,school也是一个小json,所以它也需要从一个小json转换成一个model,也需要一个transformer。
由于JSONKeyPathsByPropertyKey
可以自定义属性以及json中字段的对应关系,所以Mantle比KVC好的一点就是可以property和json字段不一定必须名字一致哈。
3. JSONModel
git: https://github.com/jsonmodel/jsonmodel
JSONModel和KVC类似,它也不需要.m文件里面干点儿啥,只要一句话就能搞成model。但是model类需要继承JSONModel哦~
#import <Foundation/Foundation.h>
@import JSONModel;
NS_ASSUME_NONNULL_BEGIN
@interface JsonSchoolModel : JSONModel
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *location;
@end
@interface JsonPersonModel : JSONModel
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *age;
@property (nonatomic, strong) JsonSchoolModel *school;
@end
NS_ASSUME_NONNULL_END
====================
#import "JsonPersonModel.h"
@implementation JsonSchoolModel
- (NSString *)description {
return [NSString stringWithFormat:@"name:%@, location:%@", self.name, self.location] ;
}
@end
@implementation JsonPersonModel
- (NSString *)description {
return [NSString stringWithFormat:@"name:%@, age:%@, school:%@", self.name, self.age, self.school] ;
}
@end
使用的时候:
NSDictionary *dict = [self readLocalFileWithName:@"sample"];
JsonPersonModel *p1 = [[JsonPersonModel alloc] initWithDictionary:dict error:NULL];
NSLog(@"p1: %@", p1);
输出:
2020-01-26 09:59:06.664587+0800 Example1[6093:1188064] p1: name:ying, age:25, school:name:SJTU, location:shanghai
- 如果key和property名字不一样呢?
可以使用keyMapper
来定义如何map,例如下面的官方例子就是把property名orderId对应到json里面的order_id,自动在中间加一个下划线来对应:
{
"order_id": 104,
"order_product": "Product #1",
"order_price": 12.95
}
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) NSString *orderProduct;
@property (nonatomic) float orderPrice;
@end
@implementation OrderModel
+ (JSONKeyMapper *)keyMapper
{
return [JSONKeyMapper mapperForSnakeCase];
}
@end
4. MJExtension
git:https://github.com/CoderMJLee/MJExtension
MJExtension作为网评最好的一款converter,它和JSONModel类似,无需.m文件支持,甚至你的model类都不需要继承JSONModel,只要用正常的model,然后convert的时候用mj_objectWithKeyValues
即可。
所以举例里面的model用KVC里面即可,转换用以下代码:
#import <MJExtension/MJExtension.h>
NSDictionary *dict = [self readLocalFileWithName:@"sample"];
JsonPersion *p1 = [JsonPersion mj_objectWithKeyValues:dict];
NSLog(@"p1: %@", p1);
输出:
2020-01-26 12:02:49.662849+0800 Example1[6285:1246846] p1: name:ying, age:25, school:name:SJTU, location:shanghai
如果key和property名字不一致,例如将name属性名改成name2:
NSDictionary *dict = [self readLocalFileWithName:@"sample"];
[JsonPersion mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"name2":@"name"};
}];
JsonPersion *p1 = [JsonPersion mj_objectWithKeyValues:dict];
NSLog(@"p1: %@", p1);
输出:
2020-01-26 12:14:30.028732+0800 Example1[6307:1251963] p1: name2:ying, age:25, school:name:SJTU, location:shanghai
如果字段是NSDate,但是json里面是string,也可以通过重写mj_newValueFromOldValue
来实现转换:
- (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;
}
如果json里面包含array,就是下面酱紫:
[StatusResult mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"statuses" : @"Status",
// @"statuses" : [Status class],
@"ads" : @"Ad"
// @"ads" : [Ad class]
};
}];
5. YYModel
git: https://github.com/ibireme/YYModel
围观膜拜大神~ YY系列真的超厉害,它用起来也是不用.m干啥,直接用就行,model就用KVC里面的:
#import <NSObject+YYModel.h>
NSDictionary *dict = [self readLocalFileWithName:@"sample"];
JsonPersion *p1 = [JsonPersion modelWithDictionary:dict];
NSLog(@"p1: %@", p1);
输出:
2020-01-26 12:35:51.598271+0800 Example1[6320:1258252] p1: name:ying, age:25, school:name:SJTU, location:shanghai
注意无论是model没定义json里面的相应property,或是json里面少了property相应的key,都已可以正常编译过不会crash的哦~
YYModel还可以自动转换数据格式吼~ 例如model的property是NSDate,只要json里面的value符合date的格式就会被自动转换吼。
JSON/Dictionary | Model |
---|---|
NSString | NSNumber,NSURL,SEL,Class |
NSNumber | NSString |
NSString/NSNumber | C number (BOOL,int,float,NSUInteger,UInt64,...) NaN and Inf will be ignored |
NSString | NSDate parsed with these formats: yyyy-MM-dd、yyyy-MM-dd HH:mm:ss、yyyy-MM-dd'T'HH:mm:ss、yyyy-MM-dd'T'HH:mm:ssZ、EEE MMM dd HH:mm:ss Z yyyy |
NSDate | NSString formatted with ISO8601:"YYYY-MM-dd'T'HH:mm:ssZ" |
NSValue | struct (CGRect,CGSize,...) |
NSNull | nil,0 |
"no","false",... | @(NO),0 |
"yes","true",... | @(YES),1 |
- 如果property和key不一致可以在model里面覆写
modelCustomPropertyMapper
:
// JSON:
{
"n":"Harry Pottery",
"p": 256,
"ext" : {
"desc" : "A book written by J.K.Rowing."
},
"ID" : 100010
}
// Model:
@interface Book : NSObject
@property NSString *name;
@property NSInteger page;
@property NSString *desc;
@property NSString *bookID;
@end
@implementation Book
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID" : @[@"id",@"ID",@"book_id"]};
}
@end
这里的嵌套可以用
xx.xxx
的方式映射为model的一个属性吼,不用必须让嵌套的内容变成单独的model。这样的做法mantle之类的也可以哈。
YYModel也是支持array之类的,感兴趣的同学可以git看一下,它的文档也很全。
6. Comparison
上图是转换同样次数所花的时间,可以看出来mantle是最慢的,MJExtension一向是号称转换效率最高并且最好用的。YYModel各项指标都非常的快,用起来也很方便~
总体而言KVC不适于property和json key名称不一致,所以不太好用;mantle必须要提供key和property的对应表,并且速度较慢;JSONModel也还挺方便的,但是model需要继承JSONModel,整体性能不如MJExtension;YYModel比MJExtension要快一些。使用上讲MJExtension、YYModel其实方便程度差不多。
但具体使用哪个还是要看情景,比如mantle自动实现了NSCopying,MJExtension和YYModel并没有让原来model继承神马,所以没有对原来的model做默认的改变。但性能和方便性而言YYmodel还是很厉害的~
最后推荐一个json直接转成model.h以及.m的工具:https://www.jianshu.com/p/7c09fcbb42c3
参考:
https://www.jianshu.com/p/d07aaae459d2
性能对比:https://www.jianshu.com/p/5d50b7d9abd2
MJExtension用法:https://www.jianshu.com/p/1efa3c2ffde3