5

我正在开发 Objective C 项目,该项目使用 RestKit 框架来解析 JSON 响应。现在我需要一些关于以下情况的对象映射设置帮助: JSON 响应:

    {
   "data": {
       "SOME.API.Auth": {
           "maxVersion": 2,
           "minVersion": 1,
           "path": "auth.cgi"
       },
       "SOME.Station": {
           "maxVersion": 1,
           "minVersion": 1,
           "path": "Station/task.cgi"
       }
   },
   "success": true
}

和以下对象:

@interface Response : NSObject
@property (strong, nonatomic) NSArray *data;
@property (assign, nonatomic) BOOL success;
@end


@interface SomeAPIInfo : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *path;
@property (strong, nonatomic) NSString *minVersion;
@property (strong, nonatomic) NSString *maxVersion;
@end

这是我的映射设置:

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"success": @"success"}];

RKObjectMapping *dataObjectMapping =  [RKObjectMapping mappingForClass:[SomeAPIInfo class]];
dataObjectMapping.forceCollectionMapping = YES;
[dataObjectMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"];
[dataObjectMapping addAttributeMappingsFromDictionary:@{
@"(name).path": @"path",
@"(name).minVersion": @"minVersion",
@"(name).maxVersion": @"maxVersion"}];


[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data"
                                                                               toKeyPath:@"data"
                                                                             withMapping:dataObjectMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                  pathPattern:nil
                                                                                      keyPath:nil
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[_objectManager addResponseDescriptor:responseDescriptor];

问题是“数据”对象未正确映射: NSArray *data; 填充了 2 个“SomeAPIInfo”对象。名称已正确填写,但其他值(路径、最大版本、最小版本)为空。

我究竟做错了什么 ?还有另一种映射“数据”对象的方法吗?也许直接进入 NSDictionary,所以“Some.API.Auth”是关键,“SomeAPIInfo”是对象(没有“name”属性)。

感谢帮助!

编辑:我认为映射不起作用,因为键中的点(“SOME.API.Auth)。

RKMappingOperation.m:
- (NSArray *)simpleAttributeMappings
{
    NSMutableArray *mappings = [NSMutableArray array];
    for (RKAttributeMapping *mapping in self.nestedAttributeMappings) {
        if ([mapping.sourceKeyPath rangeOfString:@"."].location == NSNotFound) {
            [mappings addObject:mapping];
        }
    }
4

1 回答 1

0

我遇到了由 RestKit https://github.com/RestKit/RestKit/issues/1532中的错误引起的类似问题。我会尝试通过创建具有相同结构但没有点的假 JSON 响应来消除可能的点问题,并查看问题是否仍然存在。

于 2014-01-14T16:07:22.037 回答