Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit RKObjectMapping nested json

How can I map the nested json below with RestKit RKObjectMapping? I only need to map the "data". My current code below returns (null) - Thanks!

{

    "sucess": true,
    "error": {
        "code": "",
        "message": ""
    },
    "data": [
        {
            "id": 1,
            "name": "Salad",
            "description": "Salad Desc",
            "category_id": 1
        },
        {
            "id": 2,
            "name": "Soup",
            "description": "Soup Desc",
            "category_id": 1
        },
        {
            "id": 3,
            "name": "Wrap",
            "description": "Wrap Desc",
            "category_id": 1
        },
        {
            "id": 4,
            "name": "Appetizers",
            "description": "Appetizers Desc",
            "category_id": 1
        }
    ]

} 

Class

@interface Subcategory : NSObject    
    @property (nonatomic, retain) NSNumber *subcategoryID;
    @property (nonatomic, retain) NSNumber *categoryID;
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *description;
    @property (nonatomic, retain) NSString *thumbnail;
    @property (nonatomic, retain) NSArray *data;

ViewController

RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Subcategory class]];

RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Subcategory class]];
[dataMapping mapKeyPath:@"id" toAttribute:@"subcategoryID"];
[dataMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
[dataMapping mapAttributes:@"name", @"description", nil];

[mapping mapKeyPath:@"data" toRelationship:@"data" withMapping:dataMapping];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[NSString stringWith
like image 761
steamboy Avatar asked Apr 16 '12 00:04

steamboy


1 Answers

You are missing the rootKeyPath and also setting the mapping in the provider:

RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Subcategory class]];    
RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Subcategory class]];
[dataMapping mapKeyPath:@"id" toAttribute:@"subcategoryID"];
[dataMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
[dataMapping mapAttributes:@"name", @"description", nil];
dataMapping.rootKeyPath = @"data"; //this is what you were missing    
//get your  mapping provider (i'm not sure if you already have on declared)
[mappingProvider setObjectMapping:dataMapping forResourcePathPattern:@"/somepath/toservice/"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/somepath/toservice/"];

Here's more info to answer your question about getting to the other elements (for example 'error'):

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
    NSError *parseError = nil;
    id response = [objectLoader.response parsedBody:&parseError];
    DLog(@"response %@", response);    
    if (!parseError) {
        NSString *errorMessage = [[response valueForKey:@"error"] valueForKey:@"message"];     
        //do something with it    
    }
}
like image 80
shawnwall Avatar answered Nov 12 '22 03:11

shawnwall