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

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

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 (copynonatomicNSString *ID;

@property (copynonatomicNSString *desc;

@property (copynonatomicNSString *nowName;

@property (copynonatomicNSString *oldName;

@property (copynonatomicNSString *nameChangedTime;

@property (strongnonatomicBag *bag;

@end

@interface Bag : NSObject

@property (copynonatomicNSString *name;

@property (assignnonatomicdouble 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);

 

原文地址:https://www.cnblogs.com/huoran1120/p/5470480.html