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) {
...
}