NSDictionary *dict = @{@"name" : @"黄堡钦", @"address" : @"洪文六里"};
HBQUser *user = [HBQUser mj_objectWithKeyValues:dict];
NSLog(@"\n name = %@ \n address = %@\n", user.name, user.address);
NSString *jsonString = @"{\"name\":\"黄堡钦\", \"address\":\"洪文六里\"}";
HBQUser *user = [HBQUser mj_objectWithKeyValues:jsonString];
NSLog(@"\n name = %@ \n address = %@\n", user.name, user.address);
NSDictionary *dict = @{@"name" : @"X", @"firstUser" : @{@"name" : @"黄堡钦", @"address" : @"洪文六里"}};
HBQCompany *company = [HBQCompany mj_objectWithKeyValues:dict];
NSLog(@"\n company.name = %@ \n company.firstUser = %@ \n", company.name, company.firstUser);
NSDictionary *dict = @{@"name" : @"X", @"firstUser" : @{@"name" : @"黄堡钦", @"address" : @"洪文六里"}, @"users" : @[@{@"name" : @"王五", @"address" : @"洪文六里"}, @{@"name" : @"李四", @"address" : @"洪文六里"}, @{@"name" : @"张三", @"address" : @"洪文六里"}]};
HBQCompany *company = [HBQCompany mj_objectWithKeyValues:dict];
NSLog(@"\n company.name = %@ \n company.firstUser = %@ \n company.users = %@ \n", company.name, company.firstUser, company.users);
#import "HBQCompany.h"
@implementation HBQCompany
+ (NSDictionary *)mj_objectClassInArray {
return @{
@"users" : @"HBQUser"
};
}
@end
-
Model 属性名
和 JSON 的 Key
的映射(或者多级映射)
// 将 Model 中的 name 改成 NAME 到字典中取值,也能将 name 映射到字典的更深层级用点语表示
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"name" : @"NAME",
@"address" : @"ADDRESS"
};
}
NSArray *users = @[@{@"name" : @"黄堡钦1", @"address" : @"洪文六里"}, @{@"name" : @"黄堡钦2", @"address" : @"洪文六里"}, @{@"name" : @"黄堡钦3", @"address" : @"洪文六里"}, @{@"name" : @"黄堡钦4", @"address" : @"洪文六里"}, @{@"name" : @"黄堡钦5", @"address" : @"洪文六里"}];
NSArray *userArray = [HBQUser mj_objectArrayWithKeyValuesArray:users];
NSLog(@"%@", userArray);
HBQUser *user = [[HBQUser alloc] init];
user.name = @"黄堡钦";
user.address = @"洪文六里";
NSDictionary *userDict = [user mj_keyValues];
NSLog(@"%@", userDict);
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
-
NSString
转 NSDate
,nil
转 @""
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property {
if ([property.name isEqualToString:@"publisher"]) {
if (oldValue == nil) return @"";
} else if (property.type.typeClass == [NSDate class]) {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
return [fmt dateFromString:oldValue];
}
return oldValue;
}