I've got a problem with a POST-Mappping in RestKit. Basically my application needs to send a JSON-file to a server which expects the JSON to look exactly like the following example:
{
"ddata": {
"mLoad": {
"value": 13,
"unit": "%"
},
"cLoad": {
"value": 25,
"unit": "%"
}
},
"dm": "0815",
"vin": "VAPP",
"ts": "2014-09-24"
}
Unfortunately I'm not able to create a Mapping with RestKit which generates me a JSON that matches this format. Here's how the JSON looks at the moment:
{
"ddata" : [
{
"mLoad" : {
"unit" : "%",
"value" : "25"
}
},
{
"cLoad" : {
"unit" : "%",
"value" : "37"
}
}
],
"ts" : "2014-09-24",
"vin" : "VAPP",
"dm" : "0815"
}
As you can see in the result the ddata stuff is written as an array. Furthermore the outer { } of each "object" is too much.
Following my classes and the Mapping:
Mapping Class
@interface TopMapping : NSObject
@property NSString *dm;
@property NSString *vin;
@property NSString *ts;
@property NSSet *ddata;
@end
SubMapping Class
@interface SubMapping : NSObject
@property NSString *object;
@property NSString *value;
@property NSString *unit;
@end
Actual mapping
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[TopMapping class]];
[mapping addAttributeMappingsFromDictionary:@{
@"dm": @"dm",
@"vin": @"vin",
@"ts": @"ts"
}];
RKObjectMapping *subMapping = [RKObjectMapping mappingForClass:[SubMapping class]];
[subMapping setForceCollectionMapping:YES];
[subMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"object"];
[subMapping addAttributeMappingsFromDictionary:@{
@"(object).value" : @"value",
@"(object).unit" : @"unit"
}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"ddata" toKeyPath:@"ddata" withMapping:subMapping]];
Can anyone tell me what I need to change in order to get the desired result? Thanks in advance. (I've been struggling with this issue for days)
NSSetforddatainstead of anNSDictionary. AnNSSetis essentially anNSArraywith no duplicate elements. - Ian MacDonaldNSSetwithNSDictionarydoesn't work, I can't help you any further. - Ian MacDonald