我想用来Mantle
序列化一些对象到这个JSON
:
{
"name": "John Smith",
"age": 30,
"department_id":123
}
我有两个班级部门员工:
#import <Mantle/Mantle.h>
@interface Department : MTLModel <MTLJSONSerializing>
@property(nonatomic)int id;
@property(nonatomic)NSString *name;
@end
和员工类:
#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
序列化 Employee 实例时,我收到以下异常:“NSInternalInconsistencyException”,“department.id 不是 Employee 的属性。”
这里有什么问题?有没有办法将对象序列化为单个字典,而不是将部门对象嵌套在员工对象中?