Answering your question, yes, you can define relationships using RestKit, and the JSON representation must be nested, read ahead to see an example of this and how to map it to your objects.
You must follow these steps:
From the Object Mapping Documentation :
You need to parse the following JSON:
{ "articles": [ { "title": "RestKit Object Mapping Intro", "body": "This article details how to use RestKit object mapping...", "author": { "name": "Blake Watters", "email": " blake@restkit.org " }, "publication_date": "7/4/2011" }] }
Define your objects in Objective-c:
//author.h @interface Author : NSObject @property (nonatomic, retain) NSString* name; @property (nonatomic, retain) NSString* email; @end //article.h @interface Article : NSObject @property (nonatomic, retain) NSString* title; @property (nonatomic, retain) NSString* body; @property (nonatomic, retain) Author* author; //Here we use the author object! @property (nonatomic, retain) NSDate* publicationDate; @end
Define mappings:
// Create our new Author mapping RKObjectMapping* authorMapping = [RKObjectMapping mappingForClass:[Author class]]; // NOTE: When your source and destination key paths are symmetrical, you can use mapAttributes: as a shortcut [authorMapping mapAttributes:@"name", @"email", nil]; // Now configure the Article mapping RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class]]; [articleMapping mapKeyPath:@"title" toAttribute:@"title"]; [articleMapping mapKeyPath:@"body" toAttribute:@"body"]; [articleMapping mapKeyPath:@"author" toAttribute:@"author"]; [articleMapping mapKeyPath:@"publication_date" toAttribute:@"publicationDate"]; // Define the relationship mapping [articleMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping]; [[RKObjectManager sharedManager].mappingProvider setMapping:articleMapping forKeyPath:@"articles"];
I hope this can be useful for you!
source share