JSONModel是一个库,让我们能快速创建智能数据模型。

基本使用

假设我有这样一个JSON数据

{

"country":"Germany",

"dialCode": 49,

"isInEurope":YES

}

创建一个新的Objective-C类,并使其继承JSONModel类。声明与JSON键同名的属性。

#import "JSONModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface CountryModel : JSONModel

@property (nonatomic, copy) NSString* country;

@property (nonatomic, copy) NSString* dialCode;

@property (nonatomic, assign) BOOL isInEurope;

@end

NS_ASSUME_NONNULL_END

初始化数据模型。

NSError* error = nil;

CountryModel* countryModel = [[CountryModel alloc] initWithDictionary:dict error:error];

如果JSON的验证通过你在你的模型中填充的JSON所有相应的属性。JSONModel也将尝试将尽可能多的数据转换为您所期望的类型,在这个例子中,dialCode将数字转换成字符串。

网络请求数据转换模型

接口:https://news-at.zhihu.com/api/4/version/ios/2.3.0

返回数据示例:

{

"status":1,

"msg":"【更新内容】\r\n\r\n★ 多图有标记 流量壕忽略\r\n★ 出门前离线 没网也能看\r\n★ 喜欢请好评 不喜快吐槽\r\n★ 萌妹工程师 邮箱在下面\r\nmua@zhihu.com\r\n(一般人我们不告诉他)",

"latest":"2.5"

}

Objective-C类:

@interface TestModel : JSONModel

@property (nonatomic, assign) int status;

@property (nonatomic, copy) NSString* msg;

@property (nonatomic, copy) NSString* latest;

@end

请求数据:

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

NSString* string = [NSString stringWithFormat:@"https://news-at.zhihu.com/api/4/version/ios/2.3.0"];

string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSURL* url = [NSURL URLWithString:string];

NSURLRequest* request = [NSURLRequest requestWithURL:url];

NSURLSession* session = [NSURLSession sharedSession];

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

TestModel* country = [[TestModel alloc] initWithData:data error:nil];

NSLog(@"%@", country);

}];

[dataTask resume];

}

结果:

集合、嵌套型数据

接口:https://news-at.zhihu.com/api/4/news/latest

返回数据示例:

{

"date":"20221015",

"stories":[

{

"image_hue":"0xa08861",

"title":"小事 · 外婆的「江湖」",

"url":"https:\/\/daily.zhihu.com\/story\/9754027",

"hint":"VOL.1445",

"ga_prefix":"101507",

"images":[

"https:\/\/pic2.zhimg.com\/v2-c54a943ca0074ad6f85c3935d4205ce8.jpg?source=8673f162"

],

"type":0,

"id":9754027

},

{

"image_hue":"0x9c3b4b",

"title":"超长篇哆啦 A 梦漫画哪一部水平最高?",

"url":"https:\/\/daily.zhihu.com\/story\/9753999",

"hint":"Shimmer · 9 分钟阅读",

"ga_prefix":"101507",

"images":[

"https:\/\/pic2.zhimg.com\/v2-6a2b729e0ffc66150ebf865aed19cadd.jpg?source=8673f162"

],

"type":0,

"id":9753999

},

{

"image_hue":"0x311a2b",

"title":"据说宇宙大爆炸之前没有时间与空间,「没有时间、空间」是什么意思?",

"url":"https:\/\/daily.zhihu.com\/story\/9754006",

"hint":"王清扬 · 3 分钟阅读",

"ga_prefix":"101507",

"images":[

"https:\/\/pic1.zhimg.com\/v2-7b449549c16e7aa1757043c10ff51aaf.jpg?source=8673f162"

],

"type":0,

"id":9754006

},

{

"image_hue":"0x1b3533",

"title":"近视眼在老了以后真的会恢复并且中和老花眼吗?",

"url":"https:\/\/daily.zhihu.com\/story\/9754008",

"hint":"中科院物理所 · 1 分钟阅读",

"ga_prefix":"101507",

"images":[

"https:\/\/pic2.zhimg.com\/v2-352360b6eb73711a9b5ef1f76c1894e5.jpg?source=8673f162"

],

"type":0,

"id":9754008

},

{

"image_hue":"0x3b2f25",

"title":"中世纪的教士是如何敛财和升职的?",

"url":"https:\/\/daily.zhihu.com\/story\/9754018",

"hint":"莱茵行宫伯爵 · 10 分钟阅读",

"ga_prefix":"101507",

"images":[

"https:\/\/pica.zhimg.com\/v2-d7c8282951f182efcbde644bd3b5d85f.jpg?source=8673f162"

],

"type":0,

"id":9754018

},

{

"image_hue":"0x472d1b",

"title":"10 × 10 的正方形最多可放入多少个直径为 1 的圆?",

"url":"https:\/\/daily.zhihu.com\/story\/9754022",

"hint":"酱紫君 · 3 分钟阅读",

"ga_prefix":"101507",

"images":[

"https:\/\/pic1.zhimg.com\/v2-7687a394ff65690621f2934ea0fe91c3.jpg?source=8673f162"

],

"type":0,

"id":9754022

}

],

"top_stories":[

{

"image_hue":"0xa08861",

"hint":"作者 \/ 姜喵",

"url":"https:\/\/daily.zhihu.com\/story\/9754027",

"image":"https:\/\/pic1.zhimg.com\/v2-6c638b9f5b35c93c0e5c6ecb5a30611a.jpg?source=8673f162",

"title":"小事 · 外婆的「江湖」",

"ga_prefix":"101507",

"type":0,

"id":9754027

},

{

"image_hue":"0x040505",

"hint":"作者 \/ 王治钧Lawrence",

"url":"https:\/\/daily.zhihu.com\/story\/9753968",

"image":"https:\/\/pic2.zhimg.com\/v2-b55613fa89bf75a9c2dc6255cb240d96.jpg?source=8673f162",

"title":"你经历或见证过哪些「曼德拉效应」事件?",

"ga_prefix":"101407",

"type":0,

"id":9753968

},

{

"image_hue":"0x40372d",

"hint":"作者 \/ 豆子",

"url":"https:\/\/daily.zhihu.com\/story\/9753873",

"image":"https:\/\/picx.zhimg.com\/v2-c8e1fa94c269e7318a10ea1562dba69b.jpg?source=8673f162",

"title":"古代的丫鬟每天都做什么?",

"ga_prefix":"101007",

"type":0,

"id":9753873

},

{

"image_hue":"0x919399",

"hint":"作者 \/ 知乎用户",

"url":"https:\/\/daily.zhihu.com\/story\/9753797",

"image":"https:\/\/picx.zhimg.com\/v2-4fa5e85fe83822f33d039e4cacc77598.jpg?source=8673f162",

"title":"为什么饭店的大铁锅从来都不粘锅?",

"ga_prefix":"100907",

"type":0,

"id":9753797

},

{

"image_hue":"0x121924",

"hint":"作者 \/ 瞻云",

"url":"https:\/\/daily.zhihu.com\/story\/9753754",

"image":"https:\/\/picx.zhimg.com\/v2-ef9dfc5cb5b221cf487d878f19133cd9.jpg?source=8673f162",

"title":"如果没有生殖隔离地球会变成什么样?",

"ga_prefix":"100807",

"type":0,

"id":9753754

}

]

}

对于这种数据,我们需要将每一个嵌套都写成一个类。这些我们可以全部在一个文件中实现。

@protocol StoriesModel

@end

@protocol Top_StoriesModel

@end

#import

#import "JSONModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface StoriesModel : JSONModel

@property (nonatomic, copy) NSString* title;

@property (nonatomic, copy) NSString* ga_prefix;

@property (nonatomic, copy) NSString* image_hue;

@property (nonatomic, copy) NSString* type;

@property (nonatomic, assign) NSString* id;

@end

@interface Top_StoriesModel : JSONModel

@property (nonatomic, copy) NSString* title;

@property (nonatomic, copy) NSString* ga_prefix;

@property (nonatomic, copy) NSString* image_hue;

@property (nonatomic, copy) NSString* type;

@property (nonatomic, assign) NSString* id;

@end

@interface TestModel : JSONModel

@property (nonatomic, copy) NSString* data;

@property (nonatomic, copy) NSArray* stories;

@property (nonatomic, copy) NSArray* top_tories;

//@property (nonatomic, assign) int status;

//@property (nonatomic, copy) NSString* msg;

//@property (nonatomic, copy) NSString* latest;

@end

NS_ASSUME_NONNULL_END

.m文件中:

#import "TestModel.h"

@implementation StoriesModel

+ (BOOL) propertyIsOptional:(NSString *)propertyName {

return YES;

}

@end

@implementation Top_StoriesModel

+ (BOOL) propertyIsOptional:(NSString *)propertyName {

return YES;

}

@end

@implementation TestModel

+ (BOOL) propertyIsOptional:(NSString *)propertyName {

return YES;

}

@end

发送网络请求:

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

NSString* string = [NSString stringWithFormat:@"https://news-at.zhihu.com/api/4/news/latest"];

string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSURL* url = [NSURL URLWithString:string];

NSURLRequest* request = [NSURLRequest requestWithURL:url];

NSURLSession* session = [NSURLSession sharedSession];

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

TestModel* country = [[TestModel alloc] initWithData:data error:nil];

NSLog(@"%@", country.stories[0]);

}];

[dataTask resume];

}

结果:

推荐文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。