【问题标题】:RestKit response not mapped correctlyRestKit 响应未正确映射
【发布时间】:2013-12-31 12:58:00
【问题描述】:

我在 restkit 中有一个映射(非托管)对象:

@interface SynchObj : NSObject
  @property (nonatomic, copy) NSString *event_data_c;
  @property (nonatomic, copy) NSData *fields;
@end
....
 mappingDict = @{@"event_data_c" :@"event_data_c",
                 @"fields"       :@"fields",
                 };
 responseMapping = [RKObjectMapping mappingForClass:[SynchObj class]];
 [responseMapping addAttributeMappingsFromDictionary:mappingDict];

现在,当我收到回复时:

response.body={"response":{"event_data_c":"2013-12-31 12:12:43":161,"server_id":77,"fields":{"nome":"pippo","cognome":"pippo"},"queue":""}}

在RestKit操作请求的成功块中我做的:

SynchObj *item     = mappingResult.firstObject;
NSDictionary *JSON =
                    [[NSJSONSerialization JSONObjectWithData: item.fields
                                                                   options: kNilOptions
                                                                     error: &e] objectForKey:@"response"];
                    NSLog(@"WS: mapped response %@",JSON );

但 JSON 字典始终为空。但是,我可以看到 item.fields 不为空。 我错过了什么?

【问题讨论】:

  • 也许不是您正在寻找的帮助,但请查看AFNetworking:非常易于使用,并且可以进行所有 JSON 对象转换,并且维护良好且有文档记录。
  • @meaning-matters,RestKit 使用 AFNetworking,但是 AFNetworking 使用 JSON->NSDictionary,RestKit 使用 JSON->具有任意映射的自定义对象。
  • @Wain 谢谢,我太傻了。我错误地认为过时的 JSONKit。

标签: ios json restkit


【解决方案1】:

将您的属性更改为:

@property (nonatomic, copy) NSDictionary *fields;

(假设它始终是字典)。

然后 RestKit 将为您解压 JSON 并直接存储它,因此您无需在 success 块中进行任何后续操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多