RestKit POST-Mapping问题

时间:2014-10-09 16:45:20

标签: ios objective-c json restkit restkit-0.20

我在RestKit中遇到了POST-Mappping问题。基本上我的应用程序需要将JSON文件发送到服务器,该服务器期望JSON看起来与以下示例完全相同:

{
  "ddata": { 
    "mLoad": {
      "value": 13, 
      "unit": "%"
    }, 
    "cLoad": {
      "value": 25, 
      "unit": "%"
    }
  }, 
  "dm": "0815", 
  "vin": "VAPP", 
  "ts": "2014-09-24"
}

不幸的是,我无法使用RestKit创建映射,这会生成一个与此格式匹配的JSON。以下是JSON的观点:

{
  "ddata" : [
    {
      "mLoad" : {
        "unit" : "%",
        "value" : "25"
      }
    },
    {
      "cLoad" : {
        "unit" : "%",
        "value" : "37"
      }
    }
  ],
  "ts" : "2014-09-24",
  "vin" : "VAPP",
  "dm" : "0815"
}

正如您在结果中看到的那样, ddata 内容被写为数组。此外,每个"对象" 的外{ }太多了。
在我的课程和映射之后:

映射类

@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

实际映射

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]];

有谁可以告诉我需要改变什么才能获得理想的结果?提前致谢。 (我几天来一直在努力解决这个问题)

1 个答案:

答案 0 :(得分:0)

检查NSSet manual

您需要使用NSDictionary而不是NSSet。