kmithi1
kmithi1

Reputation: 1758

RestKit Map values from nested dictionary

I am getting JSON response like this

{
    "id" : 12345
    "course_name" : "history",
    "teacher" : "joy",
    "region" : {
                   "code" : "Al",
                   "name" : "Alabama"
               }
}

I have a course entity in coredata and a respective model in code as "MKCourse" this entity is like this

MKCourse
  - id
  - courseName
  - teacher
  - regionCode
  - regionName

I am setting values from nested dictionary in MKCourse like this -

mapping = [RKManagedObjectMapping mappingForClass:[self class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
    mapping.setDefaultValueForMissingAttributes = YES;
    mapping.setNilForMissingRelationships = YES;

    [mapping mapKeyPathsToAttributes:
     @"id", [self modelIdAttribute],
     @"course_name", @"courseName",
     @"teacher", @"teacher",
     @"region.code", @"regionCode",
     @"region.name", @"regionName",
     nil];

But it always set nil to regionCode and regionName. I don't know what is wrong. Is it possible to get values like this.

Upvotes: 4

Views: 1815

Answers (1)

Dmitry Khryukin
Dmitry Khryukin

Reputation: 6448

for RestKit 2.+ add the following code:

[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"region"];

and try addAttributeMappingsFromDictionary method

[mapping addAttributeMappingsFromDictionary:@{
    @"id", [self modelIdAttribute],
     @"course_name", @"courseName",
     @"teacher", @"teacher",
     @"region.code", @"regionCode",
     @"region.name", @"regionName"
}];

not sure about RestKit 1.0. maybe you can try to separate them:

[mapping mapKeyPath:@"id" toAttribute:[self modelIdAttribute]];
[mapping mapKeyPath:@"course_name" toAttribute:@"courseName"];
[mapping mapKeyPath:@"teacher" toAttribute:@"teacher"];
[mapping mapKeyPath:@"region.code" toAttribute:@"regionCode"];
[mapping mapKeyPath:@"region.name" toAttribute:@"regionName"];

Upvotes: 2

Related Questions