2013-02-17 212 views
5

我正在處理Objective C項目,該項目使用RestKit框架來解析JSON響應。 現在我需要一些幫助的對象映射設置爲以下情況: JSON響應:RestKit 0.20 - 動態嵌套對象映射

{ 
    "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」對象。名稱被正確填充,但其他值(路徑,maxVersion,minVersion)爲空。

我在做什麼錯? 有沒有另外一種方法來映射「數據」對象?也許直接進入NSDictionary,所以「Some.API.Auth」將是關鍵,「SomeAPIInfo」將是對象(沒有「名稱」屬性)。

感謝您的幫助!

編輯:我認爲映射不因爲圓點鍵(「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]; 
     } 
    } 

回答

0

我碰到了,是由一個錯誤引起的類似問題在RestKit https://github.com/RestKit/RestKit/issues/1532。我試圖通過創建一個假的JSON響應結構相同,但沒有點,並看看問題是否仍然存在,以消除可能的點問題。