json - Restkit 0.22.0 如何映射没有要发布的 keyPath 的简单 JSON 响应

标签 json restkit response restkit-0.20 rkmappingtest

为简单起见,Rest API 发送此 JSON response-body= { "status": "ok"}。

我设置了我的 Restkit 映射,比如...创建了一个名为 StatusResponse 的类,它有一个 @property (nonatomic, assign) NSString *status;

RKObjectMapping *statusResponseMapping = [RKObjectMapping mappingForClass:[StatusResponse class]];
    [statusResponseMapping addAttributeMappingsFromDictionary:@{@"status":@"status"}]; 

// also tried : [statusResponseMapping addAttributeMappingsFromDictionary:@[@"status"]]; which resulted in same error

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusResponseMapping method:RKRequestMethodPOST pathPattern:@"status" keyPath:nil statusCodes:statusCodes];

[[RKObjectManager sharedManager] addResponseDescriptor:statusResponseDescriptor];

I'm running a post which is successful, but I get this error back:

NSUnderlyingError=0x17024d440 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}
response.body={
  "status": "ok"
}

对此的任何帮助将不胜感激。

最佳答案

您应该尝试将 keyPath 设为 @""并将 pathPattern 设为 nil,这是我正在使用的一段代码,它对我来说非常适合。我正在做与您正在尝试做的完全相同的事情。

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[ResponseStatus rkObjectMappingForResponse:YES] method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:statusCodes];

关于json - Restkit 0.22.0 如何映射没有要发布的 keyPath 的简单 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122430/

相关文章:

java - 如何将错误消息添加到响应正文?

python - Pandas DataFrame 日期时间索引在 JSON 转换和重新转换后无法生存

ios - RestKit:如何在映射前预处理 JSON 值

ios - 无法映射嵌套的RestKit对象

ios - 使用 RestKit 对象映射空响应

zend-framework - 在前端 Controller 插件 Zend 中重定向

作为下载的 Node.js 输出流

java - 如何高效地搜索和更新 JsonArray 中的 JsonObject?

json - 如何计算嵌套在 Postgresql JSON 中的数字?

java - 在生成 application/json 的 Web API 中返回简单字符串