RestKit响应未正确映射

时间:2013-12-31 12:58:01

标签: ios json restkit

我在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字典始终为null。但是我可以看到item.fields不为null。 我缺少什么?

1 个答案:

答案 0 :(得分:1)

将您的财产更改为:

@property (nonatomic, copy) NSDictionary *fields;

(假设它总是字典)。

然后RestKit将为您解压缩JSON并直接存储它,这样您就不需要在success块中进行任何后续操作。