基本使用
{
"country":"Germany",
"dialCode": 49,
"isInEurope":YES
}
#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":"【更新内容】rnrn★ 多图有标记 流量壕忽略rn★ 出门前离线 没网也能看rn★ 喜欢请好评 不喜快吐槽rn★ 萌妹工程师 邮箱在下面rnmua@zhihu.comrn(一般人我们不告诉他)",
"latest":"2.5"
}
@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 <UIKit/UIKit.h>
#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<StoriesModel>* stories;
@property (nonatomic, copy) NSArray<Top_StoriesModel>* 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];
}
结果:
原文地址:https://blog.csdn.net/m0_63852285/article/details/127334824
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_48318.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。