平时写项目习惯用 JSON Accelerator 进行json 数据的处理,有时遇到问题找度娘发现对JSON Accelerator的介绍都比较笼统,这里记录下我的使用心得,希望帮助有需要的朋友
这里我就用项目里的数据进行演示,简单的json数据网上有很多教程,这里就不演示了,接下来演示下json数据中包含数组时的使用方法
{
"status" : 0,
"result" : [
{
"siteProfileDO" : {
"siteId" : 12,
"ext2" : null,
"subBizType" : null,
"gmtCreate" : 1508988069000,
"profileType" : 1,
"gmtModified" : 1509901774000,
"ext1" : null,
"createUser" : 1,
"profileName" : "乐呗",
"profileValue" : "http:\/\/uni.com\/addressMSF\/product.html",
"profileCode" : "appMiddleCarouselFigure1",
"sitePhotoId" : 53,
"bizType" : 3,
"siteProfileId" : 56,
"modifyUser" : 15,
"ext3" : null,
"status" : 1
},
"sitePhotoDO" : {
"siteId" : 12,
"digest" : null,
"photoPath" : "\/MSF_IMGS\/oem_12\/1\/",
"photoType" : 0,
"gmtCreate" : 1508988069000,
"gmtModified" : null,
"sitePhotoType" : 3,
"createUser" : 1,
"modifyUser" : null,
"sitePhotoId" : 53,
"photoName" : "site_photo_2017102611210804729.png",
"status" : 1
}
},
{
"siteProfileDO" : {
"siteId" : 12,
"ext2" : null,
"subBizType" : null,
"gmtCreate" : 1508988069000,
"profileType" : 1,
"gmtModified" : 1509901774000,
"ext1" : null,
"createUser" : 1,
"profileName" : "会员卡",
"profileValue" : "http:\/\/uniom\/addressMSF\/product.html",
"profileCode" : "appMiddleCarouselFigure2",
"sitePhotoId" : 54,
"bizType" : 3,
"siteProfileId" : 57,
"modifyUser" : 15,
"ext3" : null,
"status" : 1
},
"sitePhotoDO" : {
"siteId" : 12,
"digest" : null,
"photoPath" : "\/MSF_IMGS\/oem_12\/1\/",
"photoType" : 0,
"gmtCreate" : 1508988069000,
"gmtModified" : null,
"sitePhotoType" : 3,
"createUser" : 1,
"modifyUser" : null,
"sitePhotoId" : 54,
"photoName" : "site_photo_2017102611210904094.png",
"status" : 1
}
},
{
"siteProfileDO" : {
"siteId" : 12,
"ext2" : null,
"subBizType" : null,
"gmtCreate" : 1508988069000,
"profileType" : 1,
"gmtModified" : 1509901774000,
"ext1" : null,
"createUser" : 1,
"profileName" : "信用卡申请",
"profileValue" : "http:\/\/unicom\/addressMSF\/product.html",
"profileCode" : "appMiddleCarouselFigure3",
"sitePhotoId" : 55,
"bizType" : 3,
"siteProfileId" : 58,
"modifyUser" : 15,
"ext3" : null,
"status" : 1
},
"sitePhotoDO" : {
"siteId" : 12,
"digest" : null,
"photoPath" : "\/MSF_IMGS\/oem_12\/1\/",
"photoType" : 0,
"gmtCreate" : 1508988069000,
"gmtModified" : null,
"sitePhotoType" : 3,
"createUser" : 1,
"modifyUser" : null,
"sitePhotoId" : 55,
"photoName" : "site_photo_2017102611210970416.png",
"status" : 1
}
}
],
"paging" : null,
"countResult" : null,
"code" : null,
"message" : null,
"failures" : null
}
这个json 数据 的result 对应的是一个数组
用JSON Accelerator 生成的文件
导入头文件
#import "DataModels.h"
在需要调用的方法中
//传入字典
ImageSweepBaseClass * base = [ImageSweepBaseClass modelObjectWithDictionary:dic];
相关属性可打点调用了
下面是遍历数组
//这里的 ImageSweepResult 中包含了sitePhotoDO,siteProfileDO两个对象
//base.result 为对象中的数组
for (ImageSweepResult * result in base.result ) {
[mArray addObject:[NSString stringWithFormat:@"%@%@%@", API_SERVER,result.sitePhotoDO.photoPath, result.sitePhotoDO.photoName]];
[self.nameArray addObject:result.siteProfileDO.profileName];
[self.urlArray addObject:result.siteProfileDO.profileValue];
}
具体的取值就是这个样子,希望能帮助有需要的朋友