- 急于分享,不說(shuō)廢話了,請(qǐng)看↓。
Json數(shù)據(jù)如下
- 文字描述如下Json數(shù)據(jù)的格式:
- 字典A內(nèi)嵌數(shù)組B(infor對(duì)應(yīng)的value)
- 數(shù)組B內(nèi)嵌字典C
- 字典C又內(nèi)嵌數(shù)組D1和D2。 (D1是workerItems對(duì)應(yīng)的value;D2是imgItems對(duì)應(yīng)的value)
- 數(shù)組D1又內(nèi)嵌字典E1,E2。數(shù)組D2又內(nèi)嵌字典F1,F2
{// 字典A
"success" : true,
"msg" : "操作成功!",
"infor" : [// 數(shù)組B
{// 字典C
"bill_sn" : "201708231014474483",
"regdate" : "2017-08-23 10:14:47",
"classname" : "鐘點(diǎn)工 ",
"statetype" : "1",
"payflag" : "0",
"startdate" : "2017-09-03 10:10:00",
"needcount" : "3",
"blog_regdate" : "2017-08-23 10:14:05",
"buydate" : null,
"applycount" : "2",
"returnflag" : "0",
"name" : "鐘點(diǎn)工",
"enddate" : null,
"signup_enddate" : "2017-08-29 10:13:00",
"tagnames" : "干活吧少年",
"id" : "691",
"total_fee" : null,
"blog_id" : "1070",
"tradetype" : "0",
"workerItems" : [// 數(shù)組D1,包含兩個(gè)大字典
{// 字典E1
"replytype" : "0",
"applydate" : null,
"regdate" : "2017-08-23 10:14:47",
"hirer_reply_id" : "0",
"nickname" : "曹進(jìn)如",
"statetype" : "0",
"push_acceptdate" : null,
"hirer_replytype" : "0",
"bill_id" : "691",
"id" : "1144",
"blog_id" : "1070",
"reply_id" : "0",
"handledate" : null,
"mobile" : "18363833309",
"client_id" : "257",
"clientcount" : "1",
"checkinflag" : "0",
"acceptflag" : "0",
"acceptdate" : null,
"reason" : null,
"avatar" : "http:\/\/yimiaozhaopin.com\/uploadfiles\/2017\/07\/201707130957187653_thumb.jpg",
"servicetype" : "0",
"worker_flag" : "2",
"age" : "35",
"username" : "18363833309"
},
{// 字典E2
"replytype" : "0",
"applydate" : null,
"regdate" : "2017-08-23 10:15:40",
"hirer_reply_id" : "0",
"nickname" : "王小海",
"statetype" : "0",
"push_acceptdate" : null,
"hirer_replytype" : "0",
"bill_id" : "691",
"id" : "1145",
"blog_id" : "1070",
"reply_id" : "0",
"handledate" : null,
"mobile" : "18363830000",
"client_id" : "912",
"clientcount" : "1",
"checkinflag" : "0",
"acceptflag" : "0",
"acceptdate" : null,
"reason" : null,
"avatar" : "http:\/\/yimiaozhaopin.com\/uploadfiles\/2017\/08\/201708181728428553_thumb.jpg",
"servicetype" : "0",
"worker_flag" : "2",
"age" : "27",
"username" : "18363385018"
}
],
"imgItems" : [// 數(shù)組D2,包含兩個(gè)大字典
{// 字典F1
"id" : "2538",
"imgurlbig" : "http:\/\/yimiaozhaopin.com\/uploadfiles\/2017\/08\/201708231014046642.jpg",
"imgurl" : "http:\/\/yimiaozhaopin.com\/uploadfiles\/2017\/08\/201708231014046642_thumb.jpg",
"keytype" : "2",
"orderby" : "0",
"imgheight" : "480",
"authtype" : "0",
"keyid" : "1070",
"regdate" : "2017-08-23 10:14:05",
"client_id" : "116",
"imgwidth" : "640",
"content" : null
},
{// 字典F2
"id" : "2539",
"imgurlbig" : "http:\/\/yimiaozhaopin.com\/uploadfiles\/2017\/08\/201708231014042070.jpg",
"imgurl" : "http:\/\/yimiaozhaopin.com\/uploadfiles\/2017\/08\/201708231014042070_thumb.jpg",
"keytype" : "2",
"orderby" : "1",
"imgheight" : "640",
"authtype" : "0",
"keyid" : "1070",
"regdate" : "2017-08-23 10:14:05",
"client_id" : "116",
"imgwidth" : "359",
"content" : null
}
],
"closedate" : null,
"price" : "100.0",
"address" : "山東省日照市東港區(qū)日照街道日照大學(xué)生創(chuàng)業(yè)園",
"content" : "努力吧"
}
]
}
表視圖格式如下

系統(tǒng)返回的json數(shù)據(jù)格式.png
解析很簡(jiǎn)單:
- 正確寫(xiě)法:無(wú)論AS_ZBPostOrderDetailModel模型中嵌套了多少(模型)數(shù)組,一定只要用一個(gè)mj_setupObjectClassInArray:方法來(lái)包裝兩個(gè)模型數(shù)組。
NSArray *temArr = [info objectForKey:@"infor"];
// id用postOrderID替代,模型中直接聲明postOrderID屬性,不用再聲明id了。id為系統(tǒng)字段,所以必須替換成常規(guī)字段。
[AS_ZBPostOrderDetailModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"OrderDetailID" : @"id"};
}];
************正確步驟1:************
// AS_ZBPostOrderDetailModel模型中嵌套了兩個(gè)模型數(shù)組,一定要按照如下的格式寫(xiě),不要分開(kāi)。
// 告訴MJExtension這個(gè)框架AS_ZBPostOrderDetailModel的workerItems數(shù)組屬性中裝的是AS_ZBPostOrderWorkerItemModel 模型,imgItems數(shù)組屬性中裝的是AS_ZBPostOrderImgItemModel 模型
[AS_ZBPostOrderDetailModel mj_setupObjectClassInArray:^NSDictionary *{
return @{@"workerItems" : @"AS_ZBPostOrderWorkerItemModel",
@"imgItems" : @"AS_ZBPostOrderImgItemModel"
};
}];
************正確步驟2:************
self.Marray = [AS_ZBPostOrderDetailModel mj_objectArrayWithKeyValuesArray:temArr];
- 錯(cuò)誤寫(xiě)法(樓主經(jīng)歷過(guò)):執(zhí)行了兩次mj_setupObjectClassInArray:方法。B會(huì)覆蓋掉A.所以A就相當(dāng)于沒(méi)有轉(zhuǎn)成AS_ZBPostOrderWorkerItemModel模型
NSArray *temArr = [info objectForKey:@"infor"];
// id用postOrderID替代,直接聲明postOrderID屬性即可
[AS_ZBPostOrderDetailModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"OrderDetailID" : @"id"};
}];
// A
[AS_ZBPostOrderDetailModel mj_setupObjectClassInArray:^NSDictionary *{
return @{@"workerItems" : @"AS_ZBPostOrderWorkerItemModel",
};
}];
// B
[AS_ZBPostOrderDetailModel mj_setupObjectClassInArray:^NSDictionary *{
return @{@"imgItems" : @"AS_ZBPostOrderImgItemModel"
};
}];
self.Marray = [AS_ZBPostOrderDetailModel mj_objectArrayWithKeyValuesArray:temArr];