Restkit简单的NSDictionary映射

时间:2013-08-02 01:13:45

标签: ios objective-c restkit restkit-0.20

似乎是一项非常简单的任务,但我发现自己在努力...... 我有这个JSON响应:

{"data":
    {"badParams":
        {"arbitraryKey":"arbitraryValue",
        "arbitraryKey2":"arbitraryValue2",
        "arbitraryKey3":"arbitraryValue3" ,
        ...
        }
    }
}

我想解析对NSDictionary的回复,包含我在回复中得到的所有key:value对:

NSDictionary *response == {"arbitraryKey":"arbitraryValue",
            "arbitraryKey2":"arbitraryValue2",
            "arbitraryKey3":"arbitraryValue3",...} 

我尝试过RKDynamicMapping但是无法做到:\

有任何建议吗?

3 个答案:

答案 0 :(得分:5)

RestKit不适用于NSDictionarys。但如果有必要,你可以做一些丑陋的解决方法......

此类将字典包装在容器类中,该容器类将KVC调用转发到字典的setObject:forKey

@interface RestKitDictionaryContainer : NSObject
@property (nonatomic, strong) NSMutableDictionary * dictionary;
@end

@implementation RestKitDictionaryContainer

- (id)init
{
    self = [super init];
    if (self) {
        _dictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    [_dictionary setObject:value forKey:key];
}

-(id)valueForUndefinedKey:(NSString *)key {
    return [_dictionary objectForKey:key];
}
@end

现在你必须像往常一样做所有的映射。 在RestKit调用之后,您只需要从该类中获取字典或将其用作字典。 好吧,我觉得这很难看,不应该用,但这是你的决定......

答案 1 :(得分:3)

如果你的目标对象是NSDictionary,那么你可能比RestKit更适合使用NSJSONSerialization。使用RestKit,您实际上不会将JSON解析为字典,而是解析为本机objective-c对象,即您的数据模型。

有关设置自己的对象,关系映射和配置RestKit objectManager的更多信息,请查看RestKit Object Mapping Guide;他们有一些非常可靠的例子说明如何做到这一点。

更新:

对于简单的JSON - > NSDictionary映射不需要任何身份验证我只会使用它:

NSString *urlString = @"http://www.yourserver.com/api";
NSData *data = [NSData dataWithContentsOfURL:urlString];
NSError *error = nil;
NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

答案 2 :(得分:3)

总结@Xean的解决方案,这是工作代码片段。

RKObjectMapping *dictionaryMapping = [RKObjectMapping mappingForClass:[RestKitDictionaryContainer class]];
[dictionaryMapping addAttributeMappingsFromArray:@[@"arbitraryKey", @"arbitraryKey2", @"arbitraryKey3"]];

然后创建响应描述符:

RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:dictionaryMapping
                                             method:RKRequestMethodAny
                                        pathPattern:@"/your/api"
                                            keyPath:@"data.badParams"
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

最后请求操作部分

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSString *value = [((RestKitDictionaryContainer *)[[mappingResult dictionary] objectForKey:@"content"]).dictionary objectForKey:@"arbitraryKey"];
            //....
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];