【发布时间】:2015-05-29 05:51:48
【问题描述】:
我如何映射这个 json
{
"json": [{
"json_department": [{
"department": {
"id": 1,
"inst_id": 1,
"dept_name": "Department",
"description": "test"
},
"needDelete": true
}],
"json_subjects": [{
"subjects": [{
"id": 1,
"department_id": 1,
"subject_name": "Sub 1"
}, {
"id": 2,
"department_id": 1,
"subject_name": "Sub 2"
}],
"needDelete": true
}]
}]
}
@interface class_department : NSObject
@property(nonatomic, assign) NSInteger dept_id;
@property(nonatomic, assign) NSInteger inst_id;
@property(nonatomic, strong) NSString *dept_name;
@property(nonatomic, strong) NSString *description_;
@end
_
@interface class_department_list : NSObject
@property(nonatomic, strong) class_department *department;
@property(nonatomic, assign) BOOL needDelete;
@end
-
@interface sync_json : NSObject
@property(nonatomic, strong) NSMutableArray *json_department;
@property(nonatomic, strong) NSMutableArray *json_subjects;
@end
-
-(RKResponseDescriptor *)getResponseDescriptor
{
RKObjectMapping *class_department_mapping = [RKObjectMapping mappingForClass:[class_department class]];
[class_department_mapping addAttributeMappingsFromDictionary:@{
@"id":@"dept_id",
@"inst_id":@"inst_id",
@"dept_name":@"dept_name"
@"description":@"description_",
}];
RKObjectMapping *class_department_list_mapping = [RKObjectMapping mappingForClass:[class_department_list class]];
[class_department_list_mapping addAttributeMappingsFromDictionary:@{
@"needDelete":@"needDelete"
}];
[class_department_list_mapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"json_department.department"
toKeyPath:@"department"
withMapping:class_department_mapping]];
RKObjectMapping *json_mapping = [RKObjectMapping mappingForClass:[sync_json class]];
[json_mapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"json_department"
toKeyPath:@"json_department"
withMapping:class_department_list_mapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:json_mapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"json"
statusCodes:statusCodes];
return responseDescriptor;
}
好的,这是我用于映射 json 的代码。我得到了“needDelete”的结果,但没有映射到 class_department。请给我一个方法来做到这一点。
谢谢。 Shinurag KR
【问题讨论】:
-
已修复。谢谢 Klevison Matias
标签: ios objective-c json mapping restkit