无法使用restkit映射嵌套数组json

时间:2015-03-25 13:54:26

标签: ios objective-c json mapping restkit

我如何映射这个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

1 个答案:

答案 0 :(得分:1)

抱歉,但试着忘记这个丑陋的实现。地幔做的一切都很容易。项目:https://github.com/Mantle/Mantle示例:http://www.objc.at/mantle