0
votes

Can anyone please help me map this json object in restkit 0.20 ?

"feeds" : [
   {
    "id": 32131354,
    "caption": "Blah Blah Blah.",
    "story" : "blah blah someone blah blah someone else",
    "story_tags": {
        "0": [
            {
            "id": 21231654,
            "name": "Someone",
            "offset": 0,
            "length": 25,
            "type": "page"
            }
        ],
        "33": [
            {
            "id": 3213212313,
            "name": "Someone else",
            "offset": 33,
            "length": 12,
            "type": "page"
            }
        ]
    },
}
{
...
}
]

I would really appreciate any help. This is what am doing right now:

RKEntityMapping *feedsMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([FacebookFeed class]) inManagedObjectStore:managedObjectStore];
feedsMapping.identificationAttributes = @[@"id"];
[feedsMapping addAttributeMappingsFromDictionary:@{

                                                   @"caption" : @"caption",
                                                   @"id" : @"id",
                                                   @"story" : @"story",
                                                }];

and this is for relationship mapping

RKEntityMapping *facebookFeedStoryTagsMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([FacebookFeedStoryTag class]) inManagedObjectStore:managedObjectStore];
facebookFeedStoryTagsMapping.identificationAttributes = @[@"id"];
[facebookFeedStoryTagsMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"offset"];
[facebookFeedStoryTagsMapping addAttributeMappingsFromDictionary:@{
                                                                 @"(offset).id" : @"id",
                                                                 @"(offset).length" : @"length",
                                                                 @"(offset).name" : @"name",
                                                                 @"(offset).type" : @"type",
                                                                 }];

[feedsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"story_tags" toKeyPath:@"story_tags" withMapping:facebookFeedStoryTagsMapping]];

but i am getting the following error:

restkit.object_mapping:RKMappingOperation.m:745 Key path 'story_tags' yielded collection containing another collection rather than a collection of objects: (
    (
            {
        id = 31314654654;
        length = 25;
        name = "Someone";
        offset = 0;
        type = page;
    }
)
)
1

1 Answers

0
votes

The JSON "0": [ presents an issue because we really want a dictionary in there (that is how your mapping is written), but we actually have an array. This is the source and meaning of the error you see.

Ideally you would change the JSON...

As it stands your FacebookFeedStoryTag would need to map the entire array (and the dictionary it contains) into a transformable attribute. This is because the mapping can not handle the nested array.

You could make this transformable attribute a transient and implement a custom setter / willSave method to take the array and extract the dictionary contents. As the key names match this could be simply done using setValuesForKeysWithDictionary: (though you would need to handle the offset key).