Shinurag KR
Shinurag KR

Reputation: 141

Unable to map nested array json with restkit

How can i map this 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;

}

OKay this is code i'm using for mapping the json. i got the result for 'needDelete' but mapping to class_department is not happen. Please give me a way to done this.

Thank you. Shinurag KR

Upvotes: 0

Views: 73

Answers (1)

Klevison
Klevison

Reputation: 3484

Sorry, but try to forget this ugly implementation. Mantle do everythig pretty easy for you. Project: https://github.com/Mantle/Mantle Example: http://www.objc.at/mantle

Upvotes: 1

Related Questions