关于字典转模型中遇到的问题

今天做公司的项目,遇到一个问题,我使用ADN进行网络连接,服务器返回给我一个字典,但是我需要的内容又是字典中的一个数组。我如何才能得到我想要的字典呢?

使用字典转模型框架

这里我使用的是mj的框架MJExtension大家可以去github上下载链接 。这是个轻量级的框架,使用方式也很简单。下面我叙述下我的问题:

    found = 600;
    num = 10;
    "time_used" = "0.028";
    total = 177837;
    "total_page" = 60;
    datalist =     (
                {
            adver = 0;
            average = 10;
            bargainprice = "";
            bbinfo = 110;
            bigtypeid = 2;
            bms = "";
            bmsdis = "";
            bmsplus = 1;
            bmsplusvalue = 1;
            bmsstartprice = "";
            bmsstatus = 1;
            busshopaddr = "\U5317\U4eac\U5e02\U6d77\U6dc0\U533a\U4e2d\U5173\U6751\U4e1c\U8def1\U53f7\U9662-1";
            "busshopaddr_hl" =             (
            );
            busshopname = "\U4e2d\U897f\U9910";
            "busshopname_hl" =             (
            );
            "city_id" = 1;
            clicks = 3;
            "coupon_body" = "";
            "coupon_endtime" = 0;
            "coupon_starttime" = 0;
            "coupon_title" = "";
            "coupon_type" = "-1";
            couponid = "";
            coupontype = "";
            evaluate = "1.00";
            "group_time" = 0;
            "hx_id" = "";
            juli = 8564572;
            keyword = "\U4e2d\U897f\U9910";
            "keyword_hl" =             (
            );
            lat = "40.00181";
            "like_num" = 0;
            lon = "116.338262";
            "main_do" = "\U5404\U79cd\U5404\U6837\U5546\U54c1\U5404\U79cd\U5404\U6837\U61c2\U5531\U6b4c\U4e0d\U653e\U8fc7\U8fd8\U56de\U5bb6\U51e0\U623f\U7ba1\U5c40\U5c31\U51e0\U4e2a\U53d1\U8fc7\U706b\U51e0\U51e0\U51e0";
            "main_do_hl" =             (
            );
            "main_img" = "http://10.0.0.200:8081/shop/1/s/41/25/142267528218754.jpg";
            newcoupon = 0;
            price = "";
            quan = 0;
            quan3 = 0;
            rebate = "";
            salenum = "";
            shopid = 54cc45d8f43731dd038b8660;
            shoptime = "10:00-18:00";
            smalltypeid = 10107;
            storey = 2;
            "storey_hl" =             (
            );
            "take_out" = 0;
            "take_out_price" = 0;
            telphone = 15010183638;
            teluserphone = 15010183638;
            tuan = 0;
            verify = 0;
            weight = 0;
        } );

这是数据库返回来的json格式。我们想要的内容是datalist中的模型。为了获取这个模型废了我好长时间。现在把经验总结一下:

  • 在返回的时候最好先打印一下返回数据的类型看一下

  • 再打印一下自己想要部分是什么类型,整了半天发现我想要的模型就在返回字典的数组中

  • 发现我想要的数据是在一个数组中后,我设置了一个属性来接受这个模型数组

MJExtension的简单使用

来看一下mj提供的参考

@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end

@interface StatusResult : NSObject
/** 存放着一堆的微博数据(里面都是Status模型) */
@property (strong, nonatomic) NSMutableArray *statuses;
/** 存放着一堆的广告数据(里面都是Ad模型) */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end

@implementation StatusResult
// 实现这个方法的目的:告诉MJExtension框架statuses和ads数组里面装的是什么模型
+ (NSDictionary *)objectClassInArray
{
    return @{
         @"statuses" : [Status class],
         @"ads" : [Ad class]
    };
}
@end

NSDictionary *dict = @{
                       @"statuses" : @[
                           @{
                               @"text" : @"今天天气真不错!",
                               @"user" : @{
                                   @"name" : @"Rose",
                                   @"icon" : @"nami.png"
                               }
                            },
                           @{
                               @"text" : @"明天去旅游了",
                               @"user" : @{
                                   @"name" : @"Jack",
                                   @"icon" : @"lufy.png"
                               }
                            }
                        ],
                       @"ads" : @[
                           @{
                               @"image" : @"ad01.png",
                               @"url" : @"http://www.ad01.com"
                           },
                           @{
                               @"image" : @"ad02.png",
                               @"url" : @"http://www.ad02.com"
                           }
                       ],
                       @"totalNumber" : @"2014"
                    };

// 将字典转为StatusResult模型
StatusResult *result = [StatusResult objectWithKeyValues:dict];

NSLog(@"totalNumber=%@", result.totalNumber);
// totalNumber=2014

// 打印statuses数组中的模型属性
for (Status *status in result.statuses) {
    NSString *text = status.text;
    NSString *name = status.user.name;
    NSString *icon = status.user.icon;
    NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
}
// text=今天天气真不错!, name=Rose, icon=nami.png
// text=明天去旅游了, name=Jack, icon=lufy.png

// 打印ads数组中的模型属性
for (Ad *ad in result.ads) {
    NSLog(@"image=%@, url=%@", ad.image, ad.url);
}
// image=ad01.png, url=http://www.ad01.com
// image=ad02.png, url=http://www.ad02.com

非常重要的一段代码:

{
    return @{
         @"statuses" : [Status class],
         @"ads" : [Ad class]
    };
}

通过这种方式我得到了模型数组,这样在现实消息的使用我们就可以直接使用模型来设置内容了

 WZStore *store = self.stores[indexPath.row];
 cell.main_do.text = store.main_do;

我们这样就可以取出每一行所对应的模型了.
大概就是这样,如果新内容会继续补充.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值