Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map JSON Arrays in RestKit

I have some troubles mapping a JSON Array to RestKit. This is what the JSON File looks like:

{"issuelist":[
    {
        "issue":[
            {
                "id":1,
                "beschreibung":"",
                "name":"Test1"
            },
            {
                "id":2,
                "beschreibung":"",
                "name":"Test2"
            }
        ]
    }
]}

I am interested in the "issue"s array. This is my mapping for a single issue:

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapAttributes:@"name", @"beschreibung", nil];
        [mapping mapKeyPathsToAttributes:
                @"id", @"identifier",
                nil];
    }];

And here is how I setup my ObjectMapping

RKObjectMappingProvider *omp = [RKObjectManager sharedManager].mappingProvider;

RKObjectMapping *issueMapping = [Issue mapping];
[omp addObjectMapping:issueMapping];

[omp setObjectMapping:issueMapping forKeyPath:@"issuelist.issue"];

Unfortunately this doesn't work. I get an log output like this:


    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'name'. Transforming from type '__NSArrayI' to 'NSString'
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'name'. No strategy for transforming from '__NSArrayI' to 'NSString'
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'name to keyPath 'name' -- value is unchanged ((null))
    T restkit.object_mapping:RKObjectMappingOperation.m:322 Mapping attribute value keyPath 'beschreibung' to 'beschreibung'
    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'beschreibung'. Transforming from type '__NSArrayI' to 'NSString'
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'beschreibung'. No strategy for transforming from '__NSArrayI' to 'NSString'
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'beschreibung to keyPath 'beschreibung' -- value is unchanged ((null))
    T restkit.object_mapping:RKObjectMappingOperation.m:322 Mapping attribute value keyPath 'id' to 'identifier'
    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'id'. Transforming from type '__NSArrayI' to 'NSString'
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'id'. No strategy for transforming from '__NSArrayI' to 'NSString'
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'id to keyPath 'identifier' -- value is unchanged ((null))
    D restkit.object_mapping:RKObjectMappingOperation.m:624 Finished mapping operation successfully...

It seems as if RestKit is trying to map the whole arry in one Issue instead of creating an array of Issues. How do I need to change my mapping to correct this?

Thanks for your help!

like image 703
thalador Avatar asked Jun 22 '12 07:06

thalador


1 Answers

Try this:

RKObjectMapping* issueMapping = [RKObjectMapping mappingForClass: [Issue class] usingBlock:^(RKObjectMapping *mapping) {
    [mapping mapAttributes:@"name", @"beschreibung", nil];
    [mapping mapKeyPathsToAttributes:
     @"id", @"identifier",
     nil];
}];
issueMapping.rootKeyPath = @"issue";
[omp setObjectMaping: issueMapping forKeyPath: @"issuelist"];

This says, when issuelist keypath is encountered use the issueMapping. And then it says for every root issue, create an Issue object.

like image 80
Paul de Lange Avatar answered Sep 19 '22 14:09

Paul de Lange