iOS——JSONModel的使用与JSONModel的嵌套

news/2024/7/20 22:19:25 标签: macos, objective-c, ios, xcode

什么是JSONModel

JSONModel是一个解析JSON数据的开源库,可以将JSON数据直接解析成自定义的model

使用 JSONModel 非常简单,只需要将你的 model 类继承自 JSONModel ,而同时 model 中的属性名又恰巧可以和 JSON 数据中的 key 名字一样的话,那么非常恭喜你,你的工作已经完成90%。

JSONModel 不只使用非常方便而且还会帮你检查 JSON 数据的完整性,如果 JSON 数据不完整的话是要返回 nil 的。它还提供了基本的数据类型转换,比如服务器错将数字传成字符串的话 JSONModel 也会帮你转换成你期望的类型。

使用JSONModel之前,我们需要导入第三方库,和导入Masonry操作一样

JSONModel的简单使用

首先,我们创建一个XCode文件,并导入JSONModel类。在该文件的xcworkspace文件中进行编程。
假设我现在创建一个Model层的文件test1,然后在viewController中进行网络请求


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *urlString = [[NSString alloc] init];
    urlString = @"https://news-at.zhihu.com/api/4/version/ios/2.3.0";
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    NSLog(@"%@", urlString);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    //根据会话创建任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error == nil) {
            test1 *t = [[test1 alloc] initWithData:data error:nil];
            NSLog(@"%@", t);
        } else {
            NSLog(@"请求失败");
        }
    }];
    [dataTask resume];
}


@end

网络请求我们使用的URL是:https://news-at.zhihu.com/api/4/version/ios/2.3.0。我们先看看这个URL:
在这里插入图片描述

放在json解析器中我们可以得到以下数据:
在这里插入图片描述

为了使用JSONModel得到这些数据,我们要先在test1类中继承JSONModel为父类,然后声明上面数据相对应的属性。要注意的是:这里我们声明的属性名必须和json数据中对应的名字相同,比如在此例子中,为了得到“【更新内容】\r\n\r\n★ 多图有标记 流量壕忽略\r\n★ 出门前离线 没网也能看\r\n★ 喜欢请好评 不喜快吐槽\r\n★ 萌妹工程师 邮箱在下面\r\nmua@zhihu.com\r\n(一般人我们不告诉他)”这个字符串,我们就必须把存放该数据的属性命名为msg。


#import <UIKit/UIKit.h>
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN

@interface test1 :  JSONModel

@property (nonatomic, assign) int stauts;
@property (nonatomic, strong) NSString *msg;
@property (nonatomic, strong) NSString *latest;

@end

NS_ASSUME_NONNULL_END

在实现文件中:


#import "test1.h"

@implementation test1

//作用是不想因为服务器的某个值没有返回(nil)就使程序崩溃,我们会加关键字Optional,如果不想每一个属性都添加,我们也可以在.m文件中重写方法
+ (BOOL) propertyIsOptional:(NSString *)propertyName {
    return YES;
}

@end

接下来我们得到结果:
在这里插入图片描述

JSONModel的嵌套

在上面的那一种方法中,我们获取的json数据内容比较简单,解析后发现它只有一个字典。但是在实际的应用中,我们经常会使用由很多数组再嵌套很多字典的json数据,如下所示:
在这里插入图片描述

那按照上面的方法,我们就要再新开很多个文件去使用JSONModel获得数据吗?在这时,我们通常会选择把每一个嵌套写成一个类,但是不是分成很多个文件,而是都写在一个文件中
在这里我们再新建一个类,名叫QianTaoJSONModel,在该类中,我们首先声明网络请求中要接收数据的两个协议。需要注意,这些协议里面都没有约定任何方法,它们也不会用来实现的,其作为属性的一种标记,例如将属性添加StoriesModel协议,则JSONModel不会对这个属性进行解析、使用这种方式来进行本地数据的管理.


//声明网络请求中要接受数据的两个协议
//这里用AModel接收stories数组的数据
@protocol AModel
@end
//用BModel接收top_stories数组的数据 
@protocol BModel
@end

#import <UIKit/UIKit.h>
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN

//同样的,在这里我们定义的属性名也要和json数据中的名字相同
@interface QianTaoJSONModel : JSONModel

@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSArray<AModel>* stories;
@property (nonatomic, copy) NSArray<BModel>* top_stories;

@end

@interface AModel : JSONModel

@property (nonatomic, copy) NSString *image_hue;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *hint;
@property (nonatomic, assign) int type;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *ga_prefix;

@end

@interface BModel : JSONModel

@property (nonatomic, copy) NSString *image_hue;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *hint;
@property (nonatomic, assign) int type;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *ga_prefix;

@end

NS_ASSUME_NONNULL_END

同样的,在该文件的实现部分也需要重写propertyIsOptional方法:


#import "QianTaoJSONModel.h"

@implementation QianTaoJSONModel

+(BOOL)propertyIsOptional:(NSString *)propertyName {
    return YES;
}

@end

@implementation AModel

+(BOOL)propertyIsOptional:(NSString *)propertyName {
    return YES;
}

@end

@implementation BModel

+(BOOL)propertyIsOptional:(NSString *)propertyName {
    return YES;
}

@end

在viewController中,我们将网络请求部分的输出改为stories数组的第一个元素:


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *urlString = [[NSString alloc] init];
    urlString = @"https://news-at.zhihu.com/api/4/news/latest";
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    NSLog(@"%@", urlString);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    //根据会话创建任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error == nil) {
            QianTaoJSONModel *t = [[QianTaoJSONModel alloc] initWithData:data error:nil];
            NSLog(@"%@", t.stories[0]);
        } else {
            NSLog(@"请求失败");
        }
    }];
    [dataTask resume];
}


@end

运行结果:
在这里插入图片描述

在这里有一个问题:假设我想输出的是t的stories数组的第一个元素中的title属性,于是我决定这样调用:
在这里插入图片描述
或者这样调用:
在这里插入图片描述
但是最后却导致了运行错误,这是因为QianTaoModel是嵌套AModel,不能直接调用。我们可以再声明一下需要的被嵌套的model,然后给其赋值,就可以直接调用了

#import <UIKit/UIKit.h>
#import "test1.h"
#import "QianTaoJSONModel.h"

@interface ViewController : UIViewController

@property (nonatomic, copy) AModel *aModel;

@end
if (error == nil) {
            QianTaoJSONModel *t = [[QianTaoJSONModel alloc] initWithData:data error:nil];
            NSLog(@"%@", t.stories[0]);
            self.aModel = t.stories[0];
            NSLog(@"%@", self.aModel.title);
        } else {

在这里插入图片描述


http://www.niftyadmin.cn/n/5097106.html

相关文章

如何将前后端分离项目部署到本地的Docker Desktop容器运行并且访问

文章目录 前言 完成了客户的一个前后端分离项目&#xff0c;要求部署到客户电脑上去展示&#xff0c;那肯定不能直接把代码弄上去跑呀~~~&#xff0c;于是我就想把他们都打包部署到本地的docker容器里面&#xff0c;方便运行和访问&#xff0c;so&#xff0c;以下内容就详细介…

Adaptive AUTOSAR CM模块介绍(六)

Event订阅和缓存原理 Event事件并不是proxy定义了就可以使用&#xff0c;需要Event事件的订阅&#xff0c;一旦订阅成功了&#xff0c;有event事件的时候&#xff0c;就会收到订阅的消息。 /* brief The application expects the CM to subscribe the event. param maxSampleC…

XTU-OJ 1412-Rotate Again

大家可能也发现我是实时更新的&#xff0c;所以大家可以点赞、评论&#xff0c;如果讲解的不够详细或者看不懂的都可以留言。多给我些反馈&#xff0c;可以让我更好的编写博客内容。&#xff08;但是别催更啊~~&#xff09; 题目描述 一个44的矩阵&#xff0c;我们可以把矩阵…

SQL实现自定义排序

业务事实上&#xff0c;经常会遇到排序问题&#xff0c;对数值类型字段排序不会有很大争议&#xff0c;但是有时希望对字符型字段按要求进行排序。本文记录自定义排序的几种方法。 使用case when关键字进行限定 本方法思路是将希望排序小的字符取值为较小的数字。注意end后面…

0基础python全栈教程前言,0基础学习python难不难

近年来&#xff0c;随着人工智能的发展和高新科技的进步&#xff0c;python的火热程度有目共睹&#xff0c;其简洁的语法和丰富的第三方库&#xff0c;以及人工智能的巨大潜力和巨大的市场&#xff0c;让其未来充满了想象空间。 0基础学习python难不难 这个并不好说&#xff…

Windows系统下设置程序开机自启(WinSW)

介绍 WinSW可以将Windows上的任何程序作为系统服务进行管理&#xff0c;已达到开机自启的效果。 支持的平台 WinSW需要运行在拥有.NET Framework 4.6.1或者更新版本的Windows平台下 下载 github&#xff1a; 下载地址 百度网盘&#xff08;v2.12.0&#xff09;&#xff1a…

最短路问题

本文用到了Dijkstra算法和Floyd算法&#xff0c;要想了解这两种算法可看Floyd算法和Dijkstra算法 目录 一、最短路问题简述 二、求定点间的最短距离&#xff08;Dijkstra算法&#xff09; 1、Dijkstra(笛卡斯特拉&#xff09;算法&#xff08;1959年&#xff09;原理 2、符…

[SQL] SELECT...CASE...WHEN...THEN...是在SQL中用于进行条件判断和赋值的语句

CASE… WHEN SELECT…CASE…WHEN…THEN…是在SQL中用于进行条件判断和赋值的语句。 以下是一个代码示例&#xff1a; SELECT column1,column2,CASE WHEN condition1 THEN result1WHEN condition2 THEN result2...WHEN conditionN THEN resultNELSE resultEND AS new_column …