0

I have a JSON structure like this:

[
    {
        "id" : "1",
        "name" : "Group 1"
    },
    {
        "id" : "2",
        "name" : "Group 2"
    },
    {
        "id" : "3",
        "name" : "Group 3"
    }
]

Please pay attention it begins with [ and ends with ]. How to map this-like JSON to array of Group objects using RestKit 2?

I tried with classic:

RKObjectMapping* groupMapping = [RKObjectMapping mappingForClass:[Group class]];
[groupMapping addAttributeMappingsFromDictionary:@{
        @"id" : @"groupID",
        @"name" : @"name"
}];
return groupMapping;

Then I'm using RKMappingTest to verify mapping, but I'm usually getting error:

restkit.object_mapping:RKMappingOperation.m:440 Failed transformation of value at keyPath 'id' to representation of type 'NSString': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value...

After parsing of JSON with:

_parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"groups.json"];

_parsedJSON is an array of dictionaries.

2
  • Does it work for real but not during test? I don't recall if there is a limitation with conversion of NSString to NSNumber... Commented Dec 18, 2013 at 15:48
  • The problem is not conversion of NSString to NSNumber, but with the JSON structure. If a JSON would be in format like {"":[ <array of objects> ]} (pay attention at enclosing brackets{}), everything goes well. Commented Dec 18, 2013 at 18:15

1 Answer 1

1

Failed transformation of value at keyPath 'id'

This is because it can't convert an array to a string.

The type of test your using cannot handle this JSON because it isn't a single mapping, it's multiple mappings, and the multiplicity is not handled by the mapping class.

When the mapping runs, it calls valueForKey: on an array and gets an array back. It expects to be calling on a dictionary and getting a string (or number).

So, basically, only test mapping individual objects.

Sign up to request clarification or add additional context in comments.

3 Comments

But is there any way I can map this JSON to an array of objects? This is what I get from REST server and I can't change it. I am willing to try altering JSON just before mapping starts. Is there a hook in RestKit 2 where I can change the parsed JSON (ie, create a Dictionary which will hold an array)? Then I could map successfully.
We're talking about a test - make it appropriate. Mapping tests should only check the mapping itself. And that is a singular thing. It's possible that if you set destinationObject to a mutable array then RestKit might infer what you want, not sure on that though.
It won't map to array in that case. But I decided to test single object mapping, not an array of objects, since that works automatically then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.