目录
- JSON转字符串
- 普通字典转模型
- 模型属性有自定义的模型YYUSer
- 属性有数组(数组里自定义模型),还有字典和集合
- 字典里的key与模型里的属性名不一致
常用的几个方法:
# json转模型
+ (instancetype)yy_modelWithJSON:(id)json;
# 模型转字符串
- (NSString *)yy_modelToJSONString
# 字典转模型
+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary ;
# 声明数组、字典或者集合里的元素类型时要重写
+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass;
# 字典里的key值与模型的属性值不一致要重复
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper;
# 下面两者是属性值在两个dic与模型之间的转化方法(自己看代码吧~😂)
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic ;
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic;
代码演示和转模型注释:
一、JSON转字符串
执行代码:
// JSON ->模型 (内部实现:json -> 字典 -> 模型)
YYBook *book = [YYBook yy_modelWithJSON:@" \
{ \
\"name\": \"Harry Potter\", \
\"pages\": 512, \
\"publishDate\": \"2010-01-01\" \
}"];
//模型 -> 字符串
NSString *bookJSON = [book yy_modelToJSONString];
NSLog(@"Book: %@", bookJSON);
****二、普通字典转模型****
模型代码:
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) uint64_t pages;
@property (nonatomic, strong) NSDate *publishDate;
执行代码:
NSDictionary *dic = @{
@"name": @"Harry Potter",
@"pages": @(512),
@"publishDate": @"2010-01-01"
};
YYBook *book1 = [YYBook yy_modelWithDictionary:dic];
NSString *bookJSON1 = [book1 yy_modelToJSONString];
NSLog(@"bookJSON: %@",bookJSON1);
****三、模型属性有自定义的模型YYUSer****
模型代码:
@property (nonatomic, assign) uint64_t rid;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSDate *createTime;
@property (nonatomic, strong) YYUser *owner; #自定义模型
执行代码:
NSDictionary *dic = @{
@"rid": @(123456789),
@"name": @"YYKit",
@"createTime" : @"2011-06-09T06:24:26Z",
@"owner": @{
@"uid" : @(989898),
@"name" : @"mawenxing"
}
};
YYRepo *repo1 = [YYRepo yy_modelWithDictionary:dic];
NSLog(@"Repo1: %@\nowner.name=%@", repo1.name,repo1.owner.name);
****四、属性有数组属性里有数组(数组里自定义模型),还多字典和集合****
模型代码:
# YYAlbum.h
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSArray *photos; # Array<YYPhoto>
@property (nonatomic, strong) NSDictionary *likedUsers; # Key:name(NSString) Value:user(YYUser)
@property (nonatomic, strong) NSSet *likedUserIds; # Set<NSNumber>
# YYPhoto.h
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *desc;
# YYAlbum.m
#把数组里面带有对象的类型专门按照这个方法,这个格式写出来
-(nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass{
return @{
@"photos" : YYPhoto.class,
@"likedUsers" : YYUser.class,
@"likedUserIds" : NSNumber.class
};
}
执行代码:
///源代码这里有些成字典的,大家如果看不懂这个JSON可以看字典
YYAlbum *album = [YYAlbum yy_modelWithJSON:@" \
{ \
\"name\" : \"Happy Birthday\", \
\"photos\" : [ \
{ \
\"url\":\"http://example.com/1.png\", \
\"desc\":\"Happy~\" \
}, \
{ \
\"url\":\"http://example.com/2.png\", \
\"desc\":\"Yeah!\" \
} \
], \
\"likedUsers\" : { \
\"Jony\" : {\"uid\":10001,\"name\":\"Jony\"}, \
\"Anna\" : {\"uid\":10002,\"name\":\"Anna\"} \
}, \
\"likedUserIds\" : [10001,10002] \
}"];
NSString *albumJSON = [album yy_modelToJSONString];
NSLog(@"Album: %@", albumJSON);
****五、字典里的属性名与模型里的属性名不一致****
模型代码:
# YYMessage.h
@property (nonatomic, assign) uint64_t messageId;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSDate *time;
# YYMessage.m
/*!
* 1.该方法是 `字典里的属性Key` 和 `要转化为模型里的属性名` 不一样 而重写的
* 前:模型的属性 后:字典里的属性
*/
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
return @{@"messageId":@"i",
@"content":@"c",
@"time":@"t"};
}
/*!
* 2. 下面的两个方法 `字典里值`与`模型的值`类型不一样`需要转换`而重写的方法
* NSDate *time dic[@"t"]是double类型的的秒数
*/
/// Dic -> model
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
self.time = (NSDate *)[NSDate dateWithTimeIntervalSince1970:[dic[@"t"] doubleValue]/1000];
return YES;
}
/// model -> Dic
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
dic[@"t"] = @([self.time timeIntervalSince1970] * 1000).description;
return YES;
}
执行代码:
/*!
* 第一种:字典版
*/
NSDictionary *dic = @{@"i":@2000000001,
@"c":@"Hello",
@"t":@1437237598000};
YYMessage *message = [YYMessage yy_modelWithDictionary:dic];
NSString *messageJSON = [message yy_modelToJSONString];
NSLog(@"messageJSON: %@", messageJSON);
/*!
* 第二种:JSON版
*/
YYMessage *message = [YYMessage yy_modelWithJSON:@"{\"i\":\"2000000001\",\"c\":\"Hello\",\"t\":\"1437237598000\"}"];
NSString *messageJSON = [message yy_modelToJSONString];
NSLog(@"messageJSON: %@", messageJSON);