Mantle Objective C:将嵌套属性映射到 JSON
Mantle Objective C: mapping nested properties to JSON
我想使用 Mantle
将一些对象序列化到这个 JSON
:
{
"name": "John Smith",
"age": 30,
"department_id":123
}
我有两个 classes 部门员工:
#import <Mantle/Mantle.h>
@interface Department : MTLModel <MTLJSONSerializing>
@property(nonatomic)int id;
@property(nonatomic)NSString *name;
@end
和员工 class:
#import <Mantle/Mantle.h>
#import "Department.h"
@interface Employee : MTLModel <MTLJSONSerializing>
@property(nonatomic)NSString *name;
@property(nonatomic)int age;
@property(nonatomic)Department *department;
@end
@implementation Employee
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
@"department.id":@"department_id"
};
}
@end
when serializing an Employee instance I receive the following
exception: "NSInternalInconsistencyException", "department.id is not a
property of Employee."
这是怎么回事?有没有办法将对象序列化为单个字典,而不是将部门对象嵌套在员工对象中?
首先从您的 Employee.m 文件中删除此代码
@implementation Employee
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
@"department.id":@"department_id"
};
}
然后在你想要 serialize
Employee
对象
时使用以下命令
Employee *objEmployee = [Employee instanceFromDict:responseObject];
希望对您有用。祝一切顺利!!
好的,我是从这里得到的:
Mantle property class based on another property?
我把映射字典修改成这样
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
NSStringFromSelector(@selector(department)) : @[@"department_id"]
};
}
并添加:
+ (NSValueTransformer *)departmentJSONTransformer {
return [MTLValueTransformer transformerUsingReversibleBlock:^id(Department *department, BOOL *success, NSError *__autoreleasing *error) {
return [MTLJSONAdapter JSONDictionaryFromModel:department error:nil];
}];
}
我想使用 Mantle
将一些对象序列化到这个 JSON
:
{
"name": "John Smith",
"age": 30,
"department_id":123
}
我有两个 classes 部门员工:
#import <Mantle/Mantle.h>
@interface Department : MTLModel <MTLJSONSerializing>
@property(nonatomic)int id;
@property(nonatomic)NSString *name;
@end
和员工 class:
#import <Mantle/Mantle.h>
#import "Department.h"
@interface Employee : MTLModel <MTLJSONSerializing>
@property(nonatomic)NSString *name;
@property(nonatomic)int age;
@property(nonatomic)Department *department;
@end
@implementation Employee
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
@"department.id":@"department_id"
};
}
@end
when serializing an Employee instance I receive the following exception: "NSInternalInconsistencyException", "department.id is not a property of Employee."
这是怎么回事?有没有办法将对象序列化为单个字典,而不是将部门对象嵌套在员工对象中?
首先从您的 Employee.m 文件中删除此代码
@implementation Employee
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
@"department.id":@"department_id"
};
}
然后在你想要 serialize
Employee
对象
Employee *objEmployee = [Employee instanceFromDict:responseObject];
希望对您有用。祝一切顺利!!
好的,我是从这里得到的: Mantle property class based on another property?
我把映射字典修改成这样
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name":@"name",
@"age":@"age",
NSStringFromSelector(@selector(department)) : @[@"department_id"]
};
}
并添加:
+ (NSValueTransformer *)departmentJSONTransformer {
return [MTLValueTransformer transformerUsingReversibleBlock:^id(Department *department, BOOL *success, NSError *__autoreleasing *error) {
return [MTLJSONAdapter JSONDictionaryFromModel:department error:nil];
}];
}