什么是JSONModel
快速的解析数据为 Model 模型,支持层级嵌套 Model 模型解析,指定类型自动转化,无需手动解析,一行代码搞定!
基本使用方法
假设请求到的 JSON 串如下:
{
"id":"10",
"country":"Germany",
"dialCode": 49,
"isInEurope":true
}
那么,接下来创建一个自己的类,并继承至 JSONModel,在头文件里面进行声明所需要的 JSON 的 key 值。
#import "JSONModel.h"
@interface CountryModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope;
@end
如果传过来的 JSON 合法,你所定义的所有的属性都会与该 JSON 值相匹配,并且 JSONModel 也会尝试尽可能的转换成你所想要的数据,就像上面的例子:
- 转化 “id”,从字符串转换成 int 型
- 拷贝 country 属性的值
- 转换 dialCode ,从NSNumber 转换为NSString 值
- 最后一个是将 isInEurope 转换成 BOOL 的属性
将Model导出字典或JOSN字符串
TestModel *data = [[TestModel alloc] initWithData:data error:nil];
NSDictionary* dict = [data toDictionary];
NSString* string = [data toJSONString];
集合、嵌套型数据
响应实例:
{
"date":"20211011",
"stories":[
{
"image_hue":"0x555555",
"title":"出现低落焦虑时,怎么分辨是「短暂情绪」还是生病了?",
"url":"https:\/\/daily.zhihu.com\/story\/9741137",
"hint":"精神科医生宋崇升 · 4 分钟阅读",
"ga_prefix":"101107",
"images":[
"https:\/\/pic3.zhimg.com\/v2-e68e99fafc8f87cd3c91a17a6f835d1f.jpg?source=8673f162"
],
"type":0,
"id":9741137
},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...}
],
"top_stories":[
{
"image_hue":"0xb38d51",
"hint":"作者 \/ 男爵兔",
"url":"https:\/\/daily.zhihu.com\/story\/9741077",
"image":"https:\/\/pica.zhimg.com\/v2-ca0fe2412807776234bf0019f7276541.jpg?source=8673f162",
"title":"2021 年诺贝尔文学奖授予小说家阿卜杜勒拉扎克·古尔纳,他是谁?",
"ga_prefix":"100807",
"type":0,
"id":9741077
},
Object{...},
Object{...},
Object{...},
Object{...}
]
}
这次的数据很复杂他有嵌套, 有数组,我们应该怎样处理这种嵌套模型呢?我们对每一个要嵌套的都写成一个类,但并不需要写成多个类文件,而是只需要在一个类文件里把该有的写好就行:
@protocol StoriesModel
@end
@protocol Top_StoriesModel
@end
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface StoriesModel : JSONModel
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *ga_prefix;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *image_hue;
@property (nonatomic, copy) NSArray *images;
@property (nonatomic, copy) NSString *id;
@end
@interface Top_StoriesModel : JSONModel
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *ga_prefix;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *image_hue;
@property (nonatomic, copy) NSString *image;
@property (nonatomic, copy) NSString *id;
@end
@interface TestModel : JSONModel
@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSArray<StoriesModel> *stories;
@property (nonatomic, copy) NSArray<Top_StoriesModel> *top_stories;
@end
NS_ASSUME_NONNULL_END
在.m文件中,需要:
#import "TestModel.h"
@implementation Top_StoriesModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName {
return YES;
}
@end
@implementation StoriesModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName {
return YES;
}
@end
@implementation TestModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName {
return YES;
}
@end
其他
自动把下划线方式的命名转为驼峰命名属性:
|