我使用 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