MJExtension解析復(fù)雜的數(shù)據(jù)(模型嵌模型,即:多級(jí)嵌套)

  • 急于分享,不說(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];


備注:無(wú)論系統(tǒng)返回的JSON數(shù)據(jù)有多復(fù)雜(例如上面Json數(shù)據(jù)),一定要鎖定到字典,然后才可以對(duì)該鎖定的字典可以進(jìn)行字典轉(zhuǎn)模型。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容