【iOS开发】—— JSONModel的使用

什么是JSONModel?

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

使用JSONModel前的准备工作

与Masonry的使用相同,需要先导入第三方库,不会这个操作的可以看我之前写的**这篇文章。**
与Masonry不同的是:将pod 后面的‘Masonry’改为‘JSONModel’即可。

platform :ios, '7.0'
target 'test2' do
pod 'JSONModel'
end
//target后面的单引号里是你工程的名字

JSONModel的使用

最基本的用法

这里我用这个接口来演示:https://news-at.zhihu.com/api/4/version/ios/2.3.0请添加图片描述

第一步:创建一个继承与JSONModel的类:

这个类里用于存放有关于网络请求得到的数据。

//TestModel.h
#import "JSONModel.h"

@interface TestModel : JSONModel
@property (nonatomic, assign) int status;
@property (nonatomic, copy) NSString* msg;
@property (nonatomic, copy) NSString* latest;
@end
//TestModel.m
#import "TestModel.h"

@implementation TestModel

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

第二步:使用网络请求到的数据将model初始化

#import "ViewController.h"
#import "TestModel.h"

@interface ViewController ()

@end

@implementation ViewController

- (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

与以上不同的是:这次的数据很复杂他有嵌套, 有数组。
我们应该怎样处理这种嵌套模型呢?我们应该对每一个要嵌套的都写成一个类,但并不是意味着要写成多个类文件,而是只需要在一个类文件里把该有的写好就行 如下代码:


//TestModel.h

@protocol StoriesModel
@end

@protocol Top_StoriesModel

@end

#import "JSONModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface TestModel : JSONModel
@property (nonatomic, copy) NSString* date;
@property (nonatomic, copy) NSArray<StoriesModel>* stories;
@property (nonatomic, copy) NSArray<Top_StoriesModel>* top_stories;
@end

@interface StoriesModel : JSONModel
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* ga_prefix;
@property (nonatomic, copy) NSString* id;

@end

@interface Top_StoriesModel : JSONModel
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* ga_prefix;
@property (nonatomic, copy) NSString* id;
@end
//ViewController.h

#import "ViewController.h"
#import "TestModel.h"
//#import "Manager.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString* string = [NSString stringWithFormat:@"http://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];
}

请添加图片描述

要注意的一点是:
由于苹果官方已经默认不让开发者使用不安全的http通信协议了,而是建议开发者使用安全的https协议。所以我们要设置一下Xcode:

1.打开info.plist文件
请添加图片描述

2.点击加号添加一行请添加图片描述

3.添加选 App Transport Security Settings 这是一个数组(此时没有元素)。
请添加图片描述

4:然后在 App Transport Security Settings 添加一行 Allow Arbitrary Loads并设为YES:
请添加图片描述

5.这样就可以在项目中使用http协议进行网络请求了。

猜你喜欢

转载自blog.csdn.net/weixin_50990189/article/details/120708880