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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM