MJExtension (automatic conversion of JSON to data model)

MJExtension (automatic conversion of JSON to data model)

Arranged from: http://www.jianshu.com/p/93c242452b9b.

Reprinted from  https://www.cnblogs.com/huoran1120/p/5470480.html

 

 

1. Function of MJExtension

Dictionary-->Model 

Model-->Dictionary 

Dictionary array-->Model array 

Model array-->Dictionary array

2.pod import statement pod'MJExtension' 

3. Simple data model conversion 

Student * stu = [Student mj_objectWithKeyValues:dict];

The attribute name of the object must be consistent with the data in json

If there is a nested model in the model, use this sentence directly, and use. To access

eg:status.retweetedStatus.user.name

4. There are array attributes in the model, and the array contains other models

Implemented inside the model StatusResult

+(NSDictionary *)objectClassInArray{

  return @{

    @"statuses":@"Status",

    @"ads":@"Ad"

  } 

}

StatusResult * result = [StatusResult mj_objectWithKeyValues:dict];

This way of writing does not need to import Status and Ad header files, the name of the class is written after the colon

for(Status *status in result.statuses) traverse access

5. If the name of the attribute in the model is different from the key of the dictionary (there is also a multi-level mapping)

@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

                                           }

                                   }

                           };

Implement the following method in Student

+(NSDictionary *)mj_replacedKeyFromPropertyName{

    return @{

             @"ID" : @"id",

             @"desc" : @"desciption",

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

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

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

             @"bag" : @"other.bag"

             };

}

Multi-level mapping, the front is the custom name, and the back is the mapping relationship in the dictionary

6. Convert a dictionary array into a model array

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. Turn a model into a dictionary

NSDictionary *statusDict = status.keyValues;

8. Convert a model array into a dictionary array

NSArray *userArray = @[user1, user2];

NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray];

NSLog(@"%@", dictArray);

Guess you like

Origin blog.csdn.net/ximiaoweilai/article/details/103841468