0

我使用 RestKit 已经有一段时间了,但一些 API 在最新版本中发生了变化,我不再能够解析简单的 JSON。

这是我的有效载荷:

{
   "result":true,
   "items":[
      {
         "id":"1",
         "receiver":"11011101"
      },
      {
         "id":"2",
         "receiver":"11011101"
      }
   ]
}

如何将“项目”字典的内容解析为我创建的对象 Conversation 的实例?

使用下面的代码不起作用(对象永远不会被映射):

RKObjectMapping* conversationMapping = [RKObjectMapping mappingForClass:[Conversation class]];
[conversationMapping mapKeyPath:@"id" toAttribute:@"id"];
[conversationMapping mapKeyPath:@"receiver" toAttribute:@"receiver"];

[[RKObjectManager sharedManager].mappingProvider setMapping:conversationMapping forKeyPath:@"items"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getConversations" delegate:self];

会话课

@interface Conversation : NSObject {
    NSString *id;
    NSString *receiver; }

+ (void)objectMapping;

@property (nonatomic, strong) NSString *id; @property (nonatomic, strong) NSString *receiver;

@end

@implementation Conversation
@synthesize id;
@synthesize receiver;


+ (void)objectMapping {
    RKObjectMapping* conversationMapping = [RKObjectMapping mappingForClass:[Conversation class]];
    [conversationMapping mapKeyPath:@"id" toAttribute:@"id"];
    [conversationMapping mapKeyPath:@"receiver" toAttribute:@"receiver"];

    [[RKObjectManager sharedManager].mappingProvider setMapping:conversationMapping forKeyPath:@"items"];
}

@end
4

2 回答 2

0

您的根对象是如何定义的(包含“结果”和您的对话“项目”的对象)?这应该看起来像这样:

@interface 我的响应

@property (nonatomic, strong) NSArray* 项目;@property (nonatomic, assign) BOOL 结果;

也有适当的映射。

于 2012-09-25T14:58:05.667 回答
0

我解决了这个问题。这与 RestKit 完全无关。从服务器返回的响应的内容类型未设置为 JSON,修复该对象映射后工作正常。

于 2012-09-26T11:57:41.027 回答