2

I'm trying to send multiple objects as an array to a Server with RestKit. Unfortunately I'm not able to do so.
Following my pretty simple objects as well as the mapping for RestKit:

Example Objects

@interface MyExampleObject : NSObject

@property NSString *key;
@property NSString *value;

@end

Array-Object holding multiple of MyExampleObject

@interface MyArray : NSObject

@property NSArray *array;

@end

Mapping

RKObjectMapping *mappingObject = [RKObjectMapping mappingForClass:[MyExampleObject class]];
[mappingObject addAttributeMappingsFromDictionary:@{
                                              @"key" : @"key",
                                              @"value" : @"value"
                                              }];

RKObjectMapping *mappingArray = [RKObjectMapping mappingForClass:[MyArray class]];
[mappingArray addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"dummy" toKeyPath:@"array" mappingObject]];

If I do it this way I’ll get the following result:

{
  "dummy" : [
    {
      "key" : "MyKey1",
      "value" : "MyValue1"
    },
    {
      "key" : "MyKey2",
      "value" : "MyValue2"
    }
  ]
}

But I want only the array without a „key“. Like this:

[
    {
      "key" : "MyKey1",
      "value" : "MyValue1"
    },
    {
      "key" : "MyKey2",
      "value" : "MyValue2"
    }
]

It seemed obvious for me to change the relationshipMappingFromKeyPath to nil. But this didn't worked (got a setObjectForKey: key cannot be nil error).

What do I have to do to send multiple MyExampleObjects to my Server as a JSON-Array?

Solution:

As Wain suggested I've removed my "Top-Mapping". Following the final mapping:

RKObjectMapping *mappingObject = [RKObjectMapping mappingForClass:[MyExampleObject class]];
[mappingObject addAttributeMappingsFromDictionary:@{
                                              @"key" : @"key",
                                              @"value" : @"value"
                                              }];

And when I post the stuff to my Server I just do something like this:

NSArray *array = [NSArray arrayWithObjects:myExampleObject1, myExampleObject2, nil];
[[RKObjectManager sharedManager] postObject:array path:@"/myPath/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
...
}
3
  • Try to paste real code, or at least edit it all so class and variable names match... Commented Oct 20, 2014 at 18:57
  • thanks for posting updated solution! came with the same issue Commented Mar 14, 2016 at 17:05
  • @Wain can you give me example how to PUT object looking like first json here. I dont want to map response. Thanks Commented Jan 20, 2017 at 10:07

1 Answer 1

2

Just get rid of the MyArray object and the associated mapping and directly pass the NSArray of MyExampleObjects to the post method. RestKit will understand that it's an array of objects to map and do the right thing.

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

1 Comment

Sorry for the late response. You were absolutely right. Thanks a lot!

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.