简体   繁体   中英

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.

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM