2

似乎是一项非常容易的任务,但我发现自己很挣扎..我有这个 JSON 响应:

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

我想解析对 NSDictionary 的响应,其中包含key:value我在响应中得到的所有对:

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

我尝试了 RKDynamicMapping 但无法成功:\

有什么建议吗?

4

3 回答 3

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 之后,您只需将字典从此类中取出或将其用作字典。好吧,我认为这很丑陋,不应该使用,但这是你的决定......

于 2013-08-20T00:30:52.337 回答
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];
于 2014-04-21T02:56:28.113 回答
3

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

有关设置您自己的对象、关系映射和配置 RestKit objectManager 的更多信息,您应该查看RestKit 对象映射指南;他们有一些非常可靠的例子来说明如何做到这一点。

更新:

对于不需要任何身份验证的简单 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];
于 2013-08-02T01:41:16.070 回答