iOS 常见问题汇总

MJExtension解析复杂的数据(模型嵌模型,即:多级嵌套)

2017-08-21  本文已影响1003人  CoderZb

Json数据如下
{// 字典A
  "success" : true,
  "msg" : "操作成功!",
  "infor" : [// 数组B
    {// 字典C
      "bill_sn" : "201708231014474483",
      "regdate" : "2017-08-23 10:14:47",
      "classname" : "钟点工 ",
      "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" : "钟点工",
      "enddate" : null,
      "signup_enddate" : "2017-08-29 10:13:00",
      "tagnames" : "干活吧少年",
      "id" : "691",
      "total_fee" : null,
      "blog_id" : "1070",
      "tradetype" : "0",
      "workerItems" : [// 数组D1,包含两个大字典
        {// 字典E1
          "replytype" : "0",
          "applydate" : null,
          "regdate" : "2017-08-23 10:14:47",
          "hirer_reply_id" : "0",
          "nickname" : "曹进如",
          "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" : [// 数组D2,包含两个大字典
        {// 字典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" : "山东省日照市东港区日照街道日照大学生创业园",
      "content" : "努力吧"
    }
  ]
}

表视图格式如下

系统返回的json数据格式.png

解析很简单:

        NSArray *temArr = [info objectForKey:@"infor"];
        // id用postOrderID替代,模型中直接声明postOrderID属性,不用再声明id了。id为系统字段,所以必须替换成常规字段。
        [AS_ZBPostOrderDetailModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"OrderDetailID" : @"id"};
        }];
        ************正确步骤1:************
       // AS_ZBPostOrderDetailModel模型中嵌套了两个模型数组,一定要按照如下的格式写,不要分开。
       // 告诉MJExtension这个框架AS_ZBPostOrderDetailModel的workerItems数组属性中装的是AS_ZBPostOrderWorkerItemModel 模型,imgItems数组属性中装的是AS_ZBPostOrderImgItemModel 模型
        [AS_ZBPostOrderDetailModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"workerItems" : @"AS_ZBPostOrderWorkerItemModel",
                     @"imgItems" : @"AS_ZBPostOrderImgItemModel"
                     };
        }];
        ************正确步骤2:************
        self.Marray = [AS_ZBPostOrderDetailModel mj_objectArrayWithKeyValuesArray:temArr];

 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];


备注:无论系统返回的JSON数据有多复杂(例如上面Json数据),一定要锁定到字典,然后才可以对该锁定的字典可以进行字典转模型。

上一篇下一篇

猜你喜欢

热点阅读