【OC】JSONModel基本使用
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;@endNS_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 <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;
@endNS_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];
}
结果:
【OC】JSONModel基本使用相关推荐
- HTTPPost/AFNetWorking/JSONModel/NSPredicate
一.HTTPPost================================================ 1. POST方式发送请求 HTTP协议下默认数据发送请求方法是GET方式,若需要 ...
- java解析多层json,手把手教你怎么解析多层嵌套的JSON数据(使用JSONModel)
2018.11.14日更新 前言 没想到这篇简单介绍JSONModel的文章竟然已经破了两千阅读量,一跃成为我阅读量最高的文章,作为iOS小白的我感到万分惶恐 在这一个月的项目中,总是用到JSONMo ...
- OC swift 一些常用第三方收集整理 (第三方集合)
整理了Xcode好用的插件,包括OC和Swift,信息更详细和完整 下拉刷新 EGOTableViewPullRefresh– 最早的下拉刷新控件. SVPullToRefresh– 下拉刷新控件. ...
- 24、OC语言的动态性学习(Runtime)
文章目录 一.动态类型 二.动态绑定 三.动态加载 四.消息机制的基本原理 消息查找阶段 动态方法解析 消息转发 消息快转发 消息慢转发 消息发送以及转发机制的总结 重写respondsToSelec ...
- OC 组合实现多继承
OC无法完全先C++使用多继承,但可以采用组合的模式来代替继承模式.(协议实现)实现多继承的代码:举例现在ClassC需要继承ClassA中methodA.ClassB中methodB,具体的代码为: ...
- OC实用转换model的工具
OC实用转换model的工具 说明 这是本人写的一个专门用来将json数据直接转换生成Model文件的工具,目的是为了让你从写Model文件的繁琐过程中解脱出来,提升效率以及减少出错的几率,工具的特点 ...
- OC学习笔记之Foundation框架NSNumber、NSValue和NSDate(转)
一.NSNumber OC数组类NSArray,它只能存放 OC的对象,对于基本的数据类型确无能为力,但是实际编程中经常要把基本的数据如int.float,结构体存放的OC数组中,怎么办?这里的 NS ...
- OC学习篇之---对象的拷贝
在前一篇文章中我们说到了如何解决对象的循环引用问题:http://blog.csdn.net/jiangwei0910410003/article/details/41926369,这一篇文章我们就来 ...
- OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法
一:NSArray 和NSMutableArray 1: NSArray:不可变数组 NSArray是OC中使用的数组,只能用来存放OC对象,不能存放非OC对象如基本数据类型 它使不可变的,一旦初始化 ...
最新文章
- 我的公交一卡通用不了了-_-
- 如何自动校正服务器时间,几个常用校正服务器时间
- php 菜谱 源码,基于php的菜谱大全api调用代码实例
- 接口请求时params与data的区别
- [原理篇] Soft Regression
- Linux Shell常用技巧(九)
- jQuery-DOM操作之属性、class
- php字符串与数字比较,PHP容易被忽略而出错陷阱 数字与字符串之间的比较
- DHCP安装授权与设置分配
- 课程设计-基于SSM实现个人健康管理系统
- 一键hosts修改工具 全百科Hosts修改器
- 企业微信再进化:打通视频号上线微信客服,丰富人即服务理念
- 推荐一份Web 工程师的前端书单
- camera理论基础和工作原理
- 中国石油大学c语言程序设计答案,中国石油大学C语言程序设计第一册在线作业答案...
- vs 搭建团队项目服务器,tfs中如何创建团队项目及如何操作团队项目
- Linux cp: omitting directory错误的原因及解决办法
- CSS3变形、渐变、动画的基本使用
- spring boot 项目页面显示不出来
- unity 2d贴图