MJExtension(JSON到数据模型的自动转换)

MJExtension(JSON到数据模型的自动转换)

整理自:http://www.jianshu.com/p/93c242452b9b。

转载自 MJExtension(JSON到数据模型的自动转换) - 浪味小仙女 - 博客园

1.MJExtension的功能

字典-->模型 

模型-->字典 

字典数组-->模型数组 

模型数组-->字典数组

2.pod导入语句 pod 'MJExtension' 

3.简单的数据模型转换 

Student * stu = [Student mj_objectWithKeyValues:dict];

对象的属性名字要与json中的数据一致

如果有模型中嵌套模型也是直接用这句话,访问的时候用.来访问

eg:status.retweetedStatus.user.name

4.模型中有数组属性,数组里面装其他模型

在模型StatusResult内部实现

+(NSDictionary *)objectClassInArray{

  return @{

    @"statuses":@"Status",

    @"ads":@"Ad"

  } 

}

StatusResult * result = [StatusResult mj_objectWithKeyValues:dict];

这种写法不需要导入Status和Ad的头文件,冒号后面写的就是类的名字

for(Status *status in result.statuses)遍历访问

5.如果模型中的属性的名字和字典的key不同(还有多级映射)

@interface Student : NSObject

@property (copy, nonatomic) NSString *ID;

@property (copy, nonatomic) NSString *desc;

@property (copy, nonatomic) NSString *nowName;

@property (copy, nonatomic) NSString *oldName;

@property (copy, nonatomic) NSString *nameChangedTime;

@property (strong, nonatomic) Bag *bag;

@end

@interface Bag : NSObject

@property (copy, nonatomic) NSString *name;

@property (assign, nonatomic) double price;

@end

NSDictionary *dict = @{

                           @"id" : @"20",

                           @"desciption" : @"孩子",

                           @"name" : @{

                                   @"newName" : @"lufy",

                                   @"oldName" : @"kitty",

                                   @"info" : @{

                                           @"nameChangedTime" : @"2013-08"

                                           }

                                   },

                           @"other" : @{

                                   @"bag" : @{

                                           @"name" : @"小书包",

                                           @"price" : @100.7

                                           }

                                   }

                           };

在Student中实现下面方法

+(NSDictionary *)mj_replacedKeyFromPropertyName{

    return @{

             @"ID" : @"id",

             @"desc" : @"desciption",

             @"oldName" : @"name.oldName",

             @"nowName" : @"name.newName",

             @"nameChangedTime" : @"name.info.nameChangedTime",

             @"bag" : @"other.bag"

             };

}

多级映射,前面是自定义的名字,后面是字典中的映射关系

6.将一个字典数组转成模型数组

NSArray *dictArray = @[

                           @{

                               @"name" : @"Jack",

                               @"icon" : @"lufy.png",

                               },

                           @{

                               @"name" : @"Rose",

                               @"icon" : @"nami.png",

                               }

                           ];

NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];

for (User *user in userArray) {

        NSLog(@"name=%@, icon=%@", user.name, user.icon);

}

7.将一个模型转为字典

NSDictionary *statusDict = status.keyValues;

8.将一个模型数组转成字典数组

NSArray *userArray = @[user1, user2];

NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray];

NSLog(@"%@", dictArray);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值