1
votes

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)

1
I think the problem is that you're using an NSSet for ddata instead of an NSDictionary. An NSSet is essentially an NSArray with no duplicate elements. - Ian MacDonald
Thanks. Do you have any idea how the mapping needs to look like with a NSDictionary? - user3420815
I'm sorry; I don't know much about RestKit. If replacing NSSet with NSDictionary doesn't work, I can't help you any further. - Ian MacDonald

1 Answers

0
votes

Check NSSet manual

You need to use NSDictionary instead of NSSet.