美文网首页
解析复杂JSON

解析复杂JSON

作者: CoderCurtis | 来源:发表于2020-04-17 11:06 被阅读0次

如下,一个复杂的JSON数据:

{
  "Data": {
    "article": {
      "title": "文章",
      "searchType": 1,
      "data": [{
        "article_id": 10496,
        "title": "做好防范,让baby不再坠床",
        "content": "",
        "author": "为宝宝加营养",
        "business_brand_name": null,
        "reads": 0,
        "is_top": 0,
        "is_hot": 0,
        "is_discuss": 0,
        "is_red": 0,
        "user_id": 1020,
        "user_name": "马瑞",
        "sort_no": 99,
        "articletype_id": 0,
        "articletype": {
          "articletype_id": 1002,
          "typename": "日常生活",
          "fid": 0
        },
        "image_id": null,
        "image": {
          "file_id": 15648,
          "path": "http://xxxx.jpg",
          "grade_code": "7f631ed8-0c80-4e91-8b4a-c",
          "sort_no": 99,
          "filetype_name": "pictext"
        },
        "grade_code": "",
        "imageList": [],
        "keydes": "",
        "photo": null,
        "iscollect": 0,
        "utime": "2018-09-19T16:00:08.447",
        "likecount": 0,
        "commentlist": null,
        "statusName": "auditSuccess",
        "contentType": 1
      }]
    },
    "video": {
      "title": "视频",
      "searchType": 2,
      "data": [{
        "courses_id": 1059,
        "title": "妈咪宝贝护理",
        "content": "讲述孕期妈咪们、0-1岁宝宝的护理内容,并列出解决方案,全真人出演,专业护士操作指导,让用户更有代入感。",
        "price": 0,
        "author": "留心脚下",
        "purchaseNotes": "虚拟商品不退不换,敬请谅解",
        "is_top": 0,
        "is_hot": 1,
        "is_red": 0,
        "is_discuss": 0,
        "is_concentration": 1,
        "user": {
          "userdtl": null,
          "coachUser": null,
          "businessUser": null,
          "user_id": 1146,
          "phone": "13648953657",
          "name": "留心脚下",
          "token": "3C76F191825EF5302BBC10609A903C77",
          "is_enable": 1,
          "ctime": "2018-09-03T20:04:54.153",
          "utime": "2018-09-03T20:04:54.153",
          "grade": {
            "grade_id": 1008,
            "grade_name": "家庭教练用户",
            "sort_no": 99,
            "ctime": "2018-07-04T10:59:22.067",
            "utime": "2018-07-04T10:59:22.067",
            "description": null,
            "userRole": [
              "coach-ordinary"
            ],
            "userType": "coachUser",
            "alias": "coach-ordinary",
            "perssions": null,
            "isUsed": false,
            "userCount": 0
          },
          "userType": "coachUser",
          "isFollow": 0,
          "image": {
            "file_id": 13293,
            "path": "http://xxxx.jpg",
            "grade_code": "",
            "sort_no": 99,
            "filetype_name": "pictext"
          },
          "resume": "宝贝需要护理,妈妈同样需要哦,快来了解吧",
          "nikeName": "留心脚下",
          "label": "妈咪宝贝护理"
        },
        "image": {
          "file_id": 14899,
          "path": "http://xxxxx.png",
          "grade_code": "6bddbf33-1c75-4fca-8195-9f41540fc8b7",
          "sort_no": 99,
          "filetype_name": "pictext"
        },
        "imageList": [],
        "sort_no": 99,
        "ctime": "2018-09-10T16:34:34.983",
        "utime": "2018-09-10T16:34:34.997",
        "keydes": "",
        "article_source": "原创",
        "coursesType": 2,
        "classify_id": 0,
        "classify": {
          "classify_id": 1005,
          "fid": 1002,
          "classifys": null,
          "classifyName": "生长发育",
          "ctime": "2018-06-29T10:42:34",
          "sort_no": 1,
          "classifyType": 2,
          "isUsed": false
        },
        "practicalPeople": "0-1岁",
        "totalNumber": 0,
        "studyNumber": 3,
        "vip_label": 0,
        "integral": 0,
        "discount": 1,
        "consumptionType": "free",
        "consumptionDetails": [],
        "score": 2.5,
        "isPurchase": 0,
        "isScore": 0,
        "comments": null,
        "progressShow": null,
        "isCollect": 0,
        "statusName": "1",
        "fcModuleType": 0
      }]
    },
    "audio": {
      "title": "音频",
      "searchType": 3,
      "data": [{
        "courses_id": 1068,
        "title": "英文儿歌",
        "content": "朗朗上口,合辙押韵,短小精悍,便于宝宝记忆,宝宝一学就会,增强了宝宝的自信心,锻炼了宝宝的记忆力与学习英语能力。",
        "price": 0,
        "author": "咿咿呀呀在唱歌",
        "purchaseNotes": "虚拟商品不退不换,敬请谅解",
        "is_top": 0,
        "is_hot": 1,
        "is_red": 0,
        "is_discuss": 0,
        "is_concentration": 1,
        "user": {
          "userdtl": null,
          "coachUser": null,
          "businessUser": null,
          "user_id": 1150,
          "phone": "13523478965",
          "name": "咿咿呀呀在唱歌",
          "token": "4F97A2CABFE986B370066730142653CE",
          "is_enable": 1,
          "ctime": "2018-09-03T20:11:53.227",
          "utime": "2018-09-03T20:11:53.227",
          "grade": {
            "grade_id": 1008,
            "grade_name": "家庭教练用户",
            "sort_no": 99,
            "ctime": "2018-07-04T10:59:22.067",
            "utime": "2018-07-04T10:59:22.067",
            "description": null,
            "userRole": [
              "coach-ordinary"
            ],
            "userType": "coachUser",
            "alias": "coach-ordinary",
            "perssions": null,
            "isUsed": false,
            "userCount": 0
          },
          "userType": "coachUser",
          "isFollow": 0,
          "image": {
            "file_id": 13297,
            "path": "http://xxxxxx.jpg",
            "grade_code": "",
            "sort_no": 99,
            "filetype_name": "pictext"
          },
          "resume": "在歌曲中学习语言,你会发现有不一样的收获",
          "nikeName": "咿咿呀呀在唱歌",
          "label": "学语儿歌"
        },
        "image": {
          "file_id": 8830,
          "path": "http://xxxx.jpg",
          "grade_code": "",
          "sort_no": 99,
          "filetype_name": "pictext"
        },
        "imageList": [],
        "sort_no": 99,
        "ctime": "2018-08-21T23:39:20.967",
        "utime": "2018-08-21T23:39:20.977",
        "keydes": "",
        "article_source": "原创",
        "coursesType": 3,
        "classify_id": 0,
        "classify": {
          "classify_id": 1016,
          "fid": 1000,
          "classifys": null,
          "classifyName": "儿歌",
          "ctime": "2018-08-07T00:00:00",
          "sort_no": 2,
          "classifyType": 3,
          "isUsed": false
        },
        "practicalPeople": "0-6岁",
        "totalNumber": 0,
        "studyNumber": 1,
        "vip_label": 0,
        "integral": 0,
        "discount": 1,
        "consumptionType": "free",
        "consumptionDetails": [],
        "score": 0,
        "isPurchase": 0,
        "isScore": 0,
        "comments": null,
        "progressShow": null,
        "isCollect": 0,
        "statusName": "1",
        "fcModuleType": 0
      }]
    },
    "activity": {
      "title": "活动",
      "searchType": 4,
      "data": [{
        "article_id": 1053,
        "title": "XXXX.株洲荷塘店 | \"绘本\",“沙滩接力跑”亲子沙龙报名开始啦",
        "content": "",
        "author": null,
        "business_brand_name": null,
        "reads": 0,
        "is_top": 0,
        "is_hot": 0,
        "is_discuss": 0,
        "is_red": 0,
        "user_id": null,
        "user_name": null,
        "sort_no": 80,
        "articletype_id": 0,
        "articletype": null,
        "image_id": null,
        "image": {
          "file_id": 10449,
          "path": "http://xxxx.jpg",
          "grade_code": "",
          "sort_no": 99,
          "filetype_name": "pictext"
        },
        "grade_code": "",
        "imageList": [],
        "keydes": null,
        "photo": null,
        "iscollect": 0,
        "utime": "0001-01-01T00:00:00",
        "likecount": 0,
        "commentlist": null,
        "statusName": null,
        "contentType": 0
      }]
    },
    "book": {
      "title": "绘本",
      "searchType": 5,
      "data": [{
        "content_id": 1582,
        "title": "爱的摇篮曲",
        "summarize": "夜晚,妈妈在床边轻声哄着小宝贝睡觉,可是小宝贝不肯睡。怎样能让宝宝入睡呢?",
        "image": {
          "file_id": 10415,
          "path": "http://xxxx.png",
          "grade_code": "",
          "sort_no": 99,
          "filetype_name": "pictext"
        },
        "ageGroupStart": 3,
        "ageGroupEnd": 3
      }]
    },
    "toy": {
      "title": "玩教具",
      "searchType": 6,
      "data": [{
        "content_id": 1629,
        "title": "赶小猪",
        "summarize": "长短适中的小棍一根,小篮球或小皮球1个。在宽阔的户外或家庭中,妈妈在地上画根线或者一个圆圈表示小猪的家,让宝宝用小棍把篮球小猪赶回家。",
        "image": {
          "file_id": 10805,
          "path": "http://xxxx.jpg",
          "grade_code": "",
          "sort_no": 99,
          "filetype_name": "pictext"
        },
        "ageGroupStart": 5,
        "ageGroupEnd": 6
      }]
    }
  }
}

如此复杂的JSON数据 不太容易看出来其结构 借助工具更好的理清其结构 网上有很多在线格式化JSON的工具 比如Json.cn


  • 最小化Data的内部最外层 可以看出Data字典分别由article、video、audio、activity、book和toy共六个子字典组成。
屏幕快照 2018-10-25 17.55.01.png
  • 接着分析第一个子字典 即article。 article由一个字符串、一个数字和一个数组data组成,data中当前有一个对象。
屏幕快照 2018-10-25 18.01.34.png
  • 继续分析article中数组data中的对象中的结构
    该对象由多个字符串、多个数字和articletype字典、image字典、imageList数组组成
屏幕快照 2018-10-25 18.04.33.png
  • articletype结构


    image.png
  • image结构


    image.png
  • imageList数组 这里数组内没有内容,可以查看接口文档获取字段


    image.png

至此,Data下第一层结构中的article已经分析完毕,剩余其他以此类推。


  • 工程中新建一个类DataModel,我以最外层的Data为DataModel

刚开始是这样的

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DataModel : NSObject

@end

NS_ASSUME_NONNULL_END
  • 然后 Data下第一层
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Article : NSObject

@end


@interface Video : NSObject

@end


@interface Audio : NSObject

@end


@interface Activity : NSObject

@end


@interface Book : NSObject

@end


@interface Toy : NSObject

@end


@interface DataModel : NSObject

@end

NS_ASSUME_NONNULL_END
  • 接着 我可能比较暴力,我直接将该JSON数据全部贴到了DataModel中,根据数据结构,哪里需要剪切到哪里,根据数据类型写property;如果觉得不好梳理,那么可以照上面讲述的一步一步分析结构然后在模型中勾画出整个模型

  • 以article为例 梳理后,是这样子的

@interface ArticleDataImageListItem : NSObject

@end

@interface ArticleDataImage : NSObject

"file_id": 15648,
"path": "http://xxxx.jpg",
"grade_code": "7f631ed8-0c80-4e91-8b4a-c",
"sort_no": 99,
"filetype_name": "pictext"

@end

@interface ArticleDataArticletype : NSObject

"articletype_id": 1002,
"typename": "日常生活",
"fid": 0

@end

@interface ArticleDataItem : NSObject

  "article_id": 10496,
  "title": "做好防范,让baby不再坠床",
  "content": "",
  "author": "为宝宝加营养",
  "business_brand_name": null,
  "reads": 0,
  "is_top": 0,
  "is_hot": 0,
  "is_discuss": 0,
  "is_red": 0,
  "user_id": 1020,
  "user_name": "马瑞",
  "sort_no": 99,
  "articletype_id": 0,
  "articletype": {
    
  },
  "image_id": null,
  "image": {
    
  },
  "grade_code": "",
  "imageList": [],
  "keydes": "",
  "photo": null,
  "iscollect": 0,
  "utime": "2018-09-19T16:00:08.447",
  "likecount": 0,
  "commentlist": null,
  "statusName": "auditSuccess",
  "contentType": 1

@end

@interface Article : NSObject

"title": "文章",
"searchType": 1,
"data": [{}]

@end
  • 对article加工后 是这样子的

PS: Int、Float、BOOL等基本数据类型均以字符串接收

@interface ArticleDataImageListItem : NSObject

@end

@interface ArticleDataImage : NSObject

@property (nonatomic, copy) NSString *file_id; //": 15648,
@property (nonatomic, copy) NSString *path; //": "http://xxxx.jpg",
@property (nonatomic, copy) NSString *grade_code; //": "7f631ed8-0c80-4e91-8b4a-c",
@property (nonatomic, copy) NSString *sort_no; //": 99,
@property (nonatomic, copy) NSString *filetype_name; //": "pictext"

@end

@interface ArticleDataArticletype : NSObject

@property (nonatomic, copy) NSString *articletype_id; //": 1002,
@property (nonatomic, copy) NSString *cc_typename; //": "日常生活",
@property (nonatomic, copy) NSString *fid; //": 0

@end

@interface ArticleDataItem : NSObject

@property (nonatomic, copy) NSString *article_id; //": 10496,
@property (nonatomic, copy) NSString *title; //": "做好防范,让baby不再坠床",
@property (nonatomic, copy) NSString *content; //": "",
@property (nonatomic, copy) NSString *author; //": "为宝宝加营养",
@property (nonatomic, copy) NSString *business_brand_name; //": null,
@property (nonatomic, copy) NSString *reads; //": 0,
@property (nonatomic, copy) NSString *is_top; //": 0,
@property (nonatomic, copy) NSString *is_hot; //": 0,
@property (nonatomic, copy) NSString *is_discuss; //": 0,
@property (nonatomic, copy) NSString *is_red; //": 0,
@property (nonatomic, copy) NSString *user_id; //": 1020,
@property (nonatomic, copy) NSString *user_name; //": "马瑞",
@property (nonatomic, copy) NSString *sort_no; //": 99,
@property (nonatomic, copy) NSString *articletype_id; //": 0,
@property (nonatomic, strong) ArticleDataArticletype *articletype; //": {},
@property (nonatomic, copy) NSString *image_id; //": null,
@property (nonatomic, strong) ArticleDataImage *image; //": {},
@property (nonatomic, copy) NSString *grade_code; //": "",
@property (nonatomic, strong) NSMutableArray<ArticleDataImageListItem *> *imageList; //": [],
@property (nonatomic, copy) NSString *keydes; //": "",
@property (nonatomic, copy) NSString *photo; //": null,
@property (nonatomic, copy) NSString *iscollect; //": 0,
@property (nonatomic, copy) NSString *utime; //": "2018-09-19T16:00:08.447",
@property (nonatomic, copy) NSString *likecount; //": 0,
@property (nonatomic, copy) NSString *commentlist; //": null,
@property (nonatomic, copy) NSString *statusName; //": "auditSuccess",
@property (nonatomic, copy) NSString *contentType; //": 1

@end

@interface Article : NSObject

@property (nonatomic, copy) NSString *title; //": "文章",
@property (nonatomic, copy) NSString *searchType; //": 1,
@property (nonatomic, strong) NSMutableArray<ArticleDataItem *> *data; //": [{}]

@end

眼睛要瞎了,仅以Article和Video为例,如下:

DataModel.h

@interface ArticleDataImageListItem : NSObject

@end

@interface ArticleDataImage : NSObject

@property (nonatomic, copy) NSString *file_id; //": 15648,
@property (nonatomic, copy) NSString *path; //": "http://xxxx.jpg",
@property (nonatomic, copy) NSString *grade_code; //": "7f631ed8-0c80-4e91-8b4a-c",
@property (nonatomic, copy) NSString *sort_no; //": 99,
@property (nonatomic, copy) NSString *filetype_name; //": "pictext"

@end

@interface ArticleDataArticletype : NSObject

@property (nonatomic, copy) NSString *articletype_id; //": 1002,
@property (nonatomic, copy) NSString *cc_typename; //": "日常生活",
@property (nonatomic, copy) NSString *fid; //": 0

@end

@interface ArticleDataItem : NSObject

@property (nonatomic, copy) NSString *article_id; //": 10496,
@property (nonatomic, copy) NSString *title; //": "做好防范,让baby不再坠床",
@property (nonatomic, copy) NSString *content; //": "",
@property (nonatomic, copy) NSString *author; //": "为宝宝加营养",
@property (nonatomic, copy) NSString *business_brand_name; //": null,
@property (nonatomic, copy) NSString *reads; //": 0,
@property (nonatomic, copy) NSString *is_top; //": 0,
@property (nonatomic, copy) NSString *is_hot; //": 0,
@property (nonatomic, copy) NSString *is_discuss; //": 0,
@property (nonatomic, copy) NSString *is_red; //": 0,
@property (nonatomic, copy) NSString *user_id; //": 1020,
@property (nonatomic, copy) NSString *user_name; //": "马瑞",
@property (nonatomic, copy) NSString *sort_no; //": 99,
@property (nonatomic, copy) NSString *articletype_id; //": 0,
@property (nonatomic, strong) ArticleDataArticletype *articletype; //": {},
@property (nonatomic, copy) NSString *image_id; //": null,
@property (nonatomic, strong) ArticleDataImage *image; //": {},
@property (nonatomic, copy) NSString *grade_code; //": "",
@property (nonatomic, strong) NSMutableArray<ArticleDataImageListItem *> *imageList; //": [],
@property (nonatomic, copy) NSString *keydes; //": "",
@property (nonatomic, copy) NSString *photo; //": null,
@property (nonatomic, copy) NSString *iscollect; //": 0,
@property (nonatomic, copy) NSString *utime; //": "2018-09-19T16:00:08.447",
@property (nonatomic, copy) NSString *likecount; //": 0,
@property (nonatomic, copy) NSString *commentlist; //": null,
@property (nonatomic, copy) NSString *statusName; //": "auditSuccess",
@property (nonatomic, copy) NSString *contentType; //": 1

@end

@interface Article : NSObject

@property (nonatomic, copy) NSString *title; //": "文章",
@property (nonatomic, copy) NSString *searchType; //": 1,
@property (nonatomic, strong) NSMutableArray<ArticleDataItem *> *data; //": [{}]

@end

/*****************************************************************/

@interface VideoDataUserGrade : NSObject

@property (nonatomic, copy) NSString *grade_id; //": 1008,
@property (nonatomic, copy) NSString *grade_name; //": "家庭教练用户",
@property (nonatomic, copy) NSString *sort_no; //": 99,
@property (nonatomic, copy) NSString *ctime; //": "2018-07-04T10:59:22.067",
@property (nonatomic, copy) NSString *utime; //": "2018-07-04T10:59:22.067",
@property (nonatomic, copy) NSString *cc_description; //": null,
@property (nonatomic, strong) NSMutableArray<NSString *> *userRole; //": [],
@property (nonatomic, copy) NSString *userType; //": "coachUser",
@property (nonatomic, copy) NSString *alias; //": "coach-ordinary",
@property (nonatomic, copy) NSString *perssions; //": null,
@property (nonatomic, copy) NSString *isUsed; //": false,
@property (nonatomic, copy) NSString *userCount; //": 0

@end

@interface VideoDataUser : NSObject

@property (nonatomic, copy) NSString *userdtl; //": null,
@property (nonatomic, copy) NSString *coachUser; //": null,
@property (nonatomic, copy) NSString *businessUser; //": null,
@property (nonatomic, copy) NSString *user_id; //": 1146,
@property (nonatomic, copy) NSString *phone; //": "13648953657",
@property (nonatomic, copy) NSString *name; //": "留心脚下",
@property (nonatomic, copy) NSString *token; //": "3C76F191825EF5302BBC10609A903C77",
@property (nonatomic, copy) NSString * is_enable; //": 1,
@property (nonatomic, copy) NSString *ctime; //": "2018-09-03T20:04:54.153",
@property (nonatomic, copy) NSString *utime; //": "2018-09-03T20:04:54.153",
@property (nonatomic, strong) VideoDataUserGrade *grade; //": {},
@property (nonatomic, copy) NSString *userType; //": "coachUser",
@property (nonatomic, copy) NSString *isFollow; //": 0,
@property (nonatomic, strong) ArticleDataImage *image; //": {},
@property (nonatomic, copy) NSString *resume; //": "宝贝需要护理,妈妈同样需要哦,快来了解吧",
@property (nonatomic, copy) NSString *nikeName; //": "留心脚下",
@property (nonatomic, copy) NSString *label; //": "妈咪宝贝护理"

@end

@interface VideoDataItemClassify : NSObject

@property (nonatomic, copy) NSString *classify_id; //": 1005,
@property (nonatomic, copy) NSString *fid; //": 1002,
@property (nonatomic, copy) NSString *classifys; //": null,
@property (nonatomic, copy) NSString *classifyName; //": "生长发育",
@property (nonatomic, copy) NSString *ctime; //": "2018-06-29T10:42:34",
@property (nonatomic, copy) NSString *sort_no; //": 1,
@property (nonatomic, copy) NSString *classifyType; //": 2,
@property (nonatomic, copy) NSString *isUsed; //": false

@end

@interface VideoDataConsumptionDetailsItem : NSObject

@end

@interface VideoDataItem : NSObject

@property (nonatomic, copy) NSString *courses_id; //": 1059,
@property (nonatomic, copy) NSString *title; //": "妈咪宝贝护理",
@property (nonatomic, copy) NSString *content; //": "讲述孕期妈咪们、0-1岁宝宝的护理内容,并列出解决方案,全真人出演,专业护士操作指导,让用户更有代入感。",
@property (nonatomic, copy) NSString *price; //": 0,
@property (nonatomic, copy) NSString *author; //": "留心脚下",
@property (nonatomic, copy) NSString *purchaseNotes; //": "虚拟商品不退不换,敬请谅解",
@property (nonatomic, copy) NSString *is_top; //": 0,
@property (nonatomic, copy) NSString *is_hot; //": 1,
@property (nonatomic, copy) NSString *is_red; //": 0,
@property (nonatomic, copy) NSString *is_discuss; //": 0,
@property (nonatomic, copy) NSString *is_concentration; //": 1,
@property (nonatomic, strong) VideoDataUser *user; //": {},
@property (nonatomic, strong) ArticleDataImage *image; //: {},
@property (nonatomic, strong) NSMutableArray<ArticleDataImageListItem *> *imageList; //": [],
@property (nonatomic, copy) NSString *sort_no; //": 99,
@property (nonatomic, copy) NSString *ctime; //": "2018-09-10T16:34:34.983",
@property (nonatomic, copy) NSString *utime; //": "2018-09-10T16:34:34.997",
@property (nonatomic, copy) NSString *keydes; //": "",
@property (nonatomic, copy) NSString *article_source; //": "原创",
@property (nonatomic, copy) NSString *coursesType; //": 2,
@property (nonatomic, copy) NSString *classify_id; //": 0,
@property (nonatomic, strong) VideoDataItemClassify *classify; //": {},
@property (nonatomic, copy) NSString *practicalPeople; //": "0-1岁",
@property (nonatomic, copy) NSString *totalNumber; //": 0,
@property (nonatomic, copy) NSString *studyNumber; //": 3,
@property (nonatomic, copy) NSString *vip_label; //": 0,
@property (nonatomic, copy) NSString *integral; //": 0,
@property (nonatomic, copy) NSString *discount; //": 1,
@property (nonatomic, copy) NSString *consumptionType; //": "free",
@property (nonatomic, strong) NSMutableArray<VideoDataConsumptionDetailsItem *> *consumptionDetails; //": [],
@property (nonatomic, copy) NSString *score; //": 2.5,
@property (nonatomic, copy) NSString *isPurchase; //": 0,
@property (nonatomic, copy) NSString *isScore; //": 0,
@property (nonatomic, copy) NSString *comments; //": null,
@property (nonatomic, copy) NSString *progressShow; //": null,
@property (nonatomic, copy) NSString *isCollect; //": 0,
@property (nonatomic, copy) NSString *statusName; //": "1",
@property (nonatomic, copy) NSString *fcModuleType; //": 0

@end

@interface Video : NSObject

@property (nonatomic, copy) NSString *title; //": "视频",
@property (nonatomic, copy) NSString *searchType; //": 2,
@property (nonatomic, strong) NSMutableArray<VideoDataItem *> *data; //": []

@end


@interface Audio : NSObject

@end


@interface Activity : NSObject

@end


@interface Book : NSObject

@end


@interface Toy : NSObject

@end


@interface DataModel : NSObject

@property (nonatomic, strong) Article *article; //": {},
@property (nonatomic, strong) Video *video; //": {},
@property (nonatomic, strong) Audio *audio; //": {},
@property (nonatomic, strong) Activity *activity; //": {},
@property (nonatomic, strong) Book *book; //": {},
@property (nonatomic, strong) Toy *toy; //": {}

@end

DataModel.m

#import "DataModel.h"

@implementation Toy

@end

@implementation Book

@end

@implementation Activity

@end

@implementation Audio

@end


@implementation VideoDataUserGrade

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"cc_description" : @[@"description"]
             };
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.userRole = [NSMutableArray array];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"userRole" : [NSString class]};
}

@end

@implementation VideoDataUser

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.grade = [[VideoDataUserGrade alloc] init];
        self.image = [[ArticleDataImage alloc] init];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"grade" : [VideoDataUserGrade class], @"image" : [ArticleDataImage class]};
}

@end

@implementation VideoDataItemClassify

@end

@implementation VideoDataConsumptionDetailsItem

@end

@implementation VideoDataItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.user = [[VideoDataUser alloc] init];
        self.image = [[ArticleDataImage alloc] init];
        self.imageList = [NSMutableArray array];
        self.classify = [[VideoDataItemClassify alloc] init];
        self.consumptionDetails = [NSMutableArray array];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"user" : [VideoDataUser class], @"image" : [ArticleDataImage class], @"imageList" : [ArticleDataImageListItem class], @"data" : [VideoDataItemClassify class], @"consumptionDetails" : [VideoDataConsumptionDetailsItem class]};
}

@end


@implementation Video

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.data = [NSMutableArray array];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"data" : [VideoDataItem class]};
}

@end

/***********/

@implementation ArticleDataImageListItem

@end

@implementation ArticleDataImage

@end

@implementation ArticleDataArticletype

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"cc_typename" : @[@"typename"]
             };
}

@end

@implementation ArticleDataItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.articletype = [[ArticleDataArticletype alloc] init];
        self.image = [[ArticleDataImage alloc] init];
        self.imageList = [NSMutableArray array];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"articletype" : [ArticleDataArticletype class], @"image" : [ArticleDataImage class], @"imageList" : [ArticleDataImageListItem class]};
}

@end

@implementation Article

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.data = [NSMutableArray array];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"data" : [ArticleDataItem class]};
}

@end

@implementation DataModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.article = [[Article alloc] init];
        self.video = [[Video alloc] init];
        self.audio = [[Audio alloc] init];
        self.activity = [[Activity alloc] init];
        self.book = [[Book alloc] init];
        self.toy = [[Toy alloc] init];
    }
    return self;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"article" : [Article class], @"video" : [Video class], @"audio" : [Audio class], @"activity" : [Activity class], @"book" : [Book class], @"toy" : [Toy class]};
}

@end

PS: 本人使用YYModel

相关文章

  • golang解析json数据

    golang官方包encoding/json包解析json数据太复杂,这里使用简单的json解析包simplejs...

  • 解析复杂JSON

    如下,一个复杂的JSON数据: 如此复杂的JSON数据 不太容易看出来其结构 借助工具更好的理清其结构 网上有很多...

  • 如何用Retrofit解析Json

    网上如何用Retrofit解析json的文章很多,但是很多都比较零散。比如如何解析复杂json格式?json格式里...

  • Android Studio 使用Gson和GsonFormat

    前言: 在日常开发中,我们可能会面对很多复杂的Json数据,每次都要费点脑筋去写解析Json的代码。Json解析的...

  • flutter 解析复杂json

    之前的文章记录了解析简单json的方法、今天遇到复杂的格式、记录作为参考 接口:http://news-at.zh...

  • swift JSON转model 使用Codable

    简单数据处理 定义模型名称 json数据定义 数据解析 复杂数据处理 定义json 定义模型 解析数据处理 列表数...

  • 使用Gson的一些感悟——解析复杂json

    使用Gson的一些感悟——解析复杂json 将json解析为泛型对象时,发现Gson有种很特别的写法,因此学习了下...

  • 2018-01-11

    Gson解析复杂json数据常用的两种解析方式 Gson gson = new Gson(); 1.gson.fr...

  • JSON

    JSON解析(反序列化) JSON解析(序列化) JSON解析补充

  • Json解析方式

    1.传统的JSON解析 1.1 生成Json 1.2 解析Json 2.Gson解析Json 2.1生成Json ...

网友评论

      本文标题:解析复杂JSON

      本文链接:https://www.haomeiwen.com/subject/ljpctqtx.html