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


    什么是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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    网络请求我们使用的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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在实现文件中:

    
    #import "test1.h"
    
    @implementation test1
    
    //作用是不想因为服务器的某个值没有返回(nil)就使程序崩溃,我们会加关键字Optional,如果不想每一个属性都添加,我们也可以在.m文件中重写方法
    + (BOOL) propertyIsOptional:(NSString *)propertyName {
        return YES;
    }
    
    @end
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

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

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    同样的,在该文件的实现部分也需要重写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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

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

    在这里有一个问题:假设我想输出的是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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    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 {
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

  • 相关阅读:
    SAE自动驾驶分级介绍
    关于编程本质那些事
    简单好用的个人图书管理系统;开源啦!北大NLP新手教程;『Rust命令行编程指南』随书代码;源代码结构可视化工具 | ShowMeAI资讯日报
    中介者模式
    linux之输出命令
    使用html2canvas转换table为图片时合并单元格rowspan失效,无边框显示问题解决(React实现)
    CDN是什么?(网络零基础入门篇)
    【无标题】
    linux基础2
    局域网无法上网主机通过TinyProxy代理主机访问公网Internet
  • 原文地址:https://blog.csdn.net/m0_73348697/article/details/133872463