平時寫項目習慣用?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];
}
具體的取值就是這個樣子,希望能幫助有需要的朋友