1. 程式人生 > >手把手教你怎麼解析多層巢狀的JSON資料(使用JSONModel)

手把手教你怎麼解析多層巢狀的JSON資料(使用JSONModel)

使用API

API介紹

參考的JSON資料(可能與你看到的不同)

{
    "date": "20181020", 
    "stories": [
        {
            "title": "每週一吸 · 狸花貓", 
            "ga_prefix": "102013", 
            "images": [
                "https://pic4.zhimg.com/v2-9ef6021d279be280f7d2ba0788e392a7.jpg"
            ], 
            "multipic": true, 
            "type": 0, 
            "id": 9698943
        }, 
        {
            "images": [
                "https://pic3.zhimg.com/v2-b752576f81b0a584bcf0e2ba96a07666.jpg"
            ], 
            "type": 0, 
            "id": 9698988, 
            "ga_prefix": "102012", 
            "title": "大誤 · 救救馬雲"
        }, 
        {
            "images": [
                "https://pic2.zhimg.com/v2-8502d8246509a273ba97133e0064176d.jpg"
            ], 
            "type": 0, 
            "id": 9698610, 
            "ga_prefix": "102010", 
            "title": "藥瓶上的「肝腎功能不全者慎用」,到底想表達什麼?"
        }, 
        {
            "images": [
                "https://pic4.zhimg.com/v2-352a7a8539dfd84227b22c8574f327b7.jpg"
            ], 
            "type": 0, 
            "id": 9698819, 
            "ga_prefix": "102009", 
            "title": "孩子你終於長大了,2 歲正是學理財的好時候"
        }, 
        {
            "images": [
                "https://pic1.zhimg.com/v2-bad6709dfc4abe08b9cacf5c75c03d00.jpg"
            ], 
            "type": 0, 
            "id": 9698931, 
            "ga_prefix": "102008", 
            "title": "「生命之橋」上,失落的人們選擇在這裡結束生命"
        }, 
        {
            "images": [
                "https://pic4.zhimg.com/v2-1b3b1acab0c685beaa5428f33c7c6c03.jpg"
            ], 
            "type": 0, 
            "id": 9698898, 
            "ga_prefix": "102007", 
            "title": "作為電影導演 / 歌手,作品資訊量這麼大,我自己都不知道"
        }, 
        {
            "images": [
                "https://pic3.zhimg.com/v2-d7a69c5474650820fce977064458d35e.jpg"
            ], 
            "type": 0, 
            "id": 9699031, 
            "ga_prefix": "102007", 
            "title": "13 歲少女肢解同窗好友,也許我們都小看她了"
        }, 
        {
            "images": [
                "https://pic2.zhimg.com/v2-abe3c9ea2cdfe8051ea36154d30b4989.jpg"
            ], 
            "type": 0, 
            "id": 9698984, 
            "ga_prefix": "102006", 
            "title": "瞎扯 · 如何正確地吐槽"
        }
    ], 
    "top_stories": [
        {
            "image": "https://pic4.zhimg.com/v2-9b704e39dc83ae7202fefc8d192e10f3.jpg", 
            "type": 0, 
            "id": 9699031, 
            "ga_prefix": "102007", 
            "title": "13 歲少女肢解同窗好友,也許我們都小看她了"
        }, 
        {
            "image": "https://pic2.zhimg.com/v2-66f79486c79deb9d779f9af99e719091.jpg", 
            "type": 0, 
            "id": 9699060, 
            "ga_prefix": "101921", 
            "title": "影視圈驚變 2018"
        }, 
        {
            "image": "https://pic4.zhimg.com/v2-e093102accf700073af6c3452293cd27.jpg", 
            "type": 0, 
            "id": 9698862, 
            "ga_prefix": "101719", 
            "title": "「56 個星座,56 支花」……等等,不是 56 個民族嗎?"
        }, 
        {
            "image": "https://pic4.zhimg.com/v2-76f03b3ab26c3d76be022a5281a13483.jpg", 
            "type": 0, 
            "id": 9698744, 
            "ga_prefix": "101810", 
            "title": "花海淪陷在抖音網紅的臀下,想要提前避免,可能真沒什麼轍"
        }, 
        {
            "image": "https://pic3.zhimg.com/v2-5babbbaa37cccaf467b996e9bc1aadda.jpg", 
            "type": 0, 
            "id": 9698809, 
            "ga_prefix": "101807", 
            "title": "大學校園淪為艾滋病重災區?一直如此,形勢也越來越嚴峻"
        }
    ]
}

參考文章(閱讀順序排列, 對於基本的不再贅述)

GitHub地址

易錯點

  • 可以看到我們要解析的這個JSON資料是相當複雜的, 他有巢狀, 有陣列, 需要一次解析, 在閱讀了上面的文章以後, 應該對如何處理有了自己的看法, 我這裡放一個我覺得比較好的的方法。
  • 首先要說明一下, JSONModel的作用說白了就是將JSON資料依照關鍵字,變成一個類的屬性, 方便使用, 它的強大之處在於能夠完成資料形式的轉換
  • 那麼我要說的是, 在巢狀資料時, 我們應該對每一個要巢狀的都寫成一個類,但並不是意味著要寫成多個類檔案,而是隻需要在一個類檔案裡把該有的寫好就行
//
//  Top_storiesJSONModel.h
//  JSONModel-exerciseDemo
//
//  Created by mac on 2018/10/19.
//  Copyright © 2018 姜凱文. All rights reserved.
//

#import "JSONModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface Top_storiesJSONModel : JSONModel

@property (nonatomic, strong) NSArray  *images;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *ga_prefix;
@property (nonatomic, strong) NSString *title;

@end

@interface StoriesJSONModel : JSONModel

@property (nonatomic, strong) NSArray  *images;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *ga_prefix;
@property (nonatomic, strong) NSString *title;

@end


@interface TotalJSONModel : JSONModel

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


@end
NS_ASSUME_NONNULL_END

//
//  Top_storiesJSONModel.m
//  JSONModel-exerciseDemo
//
//  Created by mac on 2018/10/19.
//  Copyright © 2018 姜凱文. All rights reserved.
//

#import "Top_storiesJSONModel.h"

@implementation StoriesJSONModel
@end

@implementation Top_storiesJSONModel
@end


@implementation TotalJSONModel
@end

  • 這是我的JSONmodel類,可以看看到像這樣寫就可以完成巢狀,而JSONModel本身會幫我們完成對映
  • 在接下來是使用,我是選擇使用陣列儲存Model,方便直接呼叫, 這一段一個沒什麼難度了