使用以下 JSON 响应映射 RestKit 中的对象的正确方法是什么
这个问题有 2 个部分,都与 RestKit 相关:
- 我们如何发布 2 个值电子邮件和密码,并使用对象映射器处理响应
- 我们如何在响应中映射 2 个对象
我们期待以下 JSON 响应:
{
"code" : 0,
"error_string" : "OK.",
"message" : "OK.",
"token" : {
"app_id" : "1",
"created" : "2011-08-19 11:30:31",
"token" : "ecb8862189974248233dfcc7e8fc1e4514e16972",
"user_id" : "1"
},
"user" : {
"avatar_url" : "",
"created" : "2011-08-19 11:29:21",
"email" : "[email protected]",
"forename" : "Matthew",
"gender" : "M",
}
}
什么是映射此问题的正确方法是,我们已经为 User 和 Token 设置了一个类,但是我见过的所有示例似乎都没有显示类似这样的内容,其中响应有两个部分,这是我们在片刻:
// Mapping for User
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"created" toAttribute:@"created"];
[userMapping mapKeyPath:@"avatar_url" toAttribute:@"avatarURL"];
[userMapping mapKeyPath:@"gender" toAttribute:@"gender"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"];
// Mapping for Token
RKObjectMapping* tokenMapping = [RKObjectMapping mappingForClass:[Token class]];
[tokenMapping mapAttributes:@"user_id", @"app_id", @"token", @"created", nil];
[[RKObjectManager sharedManager].mappingProvider setMapping:tokenMapping forKeyPath:@"token"];
// Load the object model via RestKit
[[[RKObjectManager sharedManager] client] setValue:@"xxxxxxxxxxxxxxxxxxx" forHTTPHeaderField:@"X-API-KEY"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/users/authenticate/" delegate:self];
感谢您在这方面提供的任何帮助,迄今为止热爱 RestKit!
There's 2 parts to this question, both about RestKit:
- How can we post up 2 values email and password, and deal with the response using the object mapper
- How can we map 2 objects in a response
We are expecting the following JSON response:
{
"code" : 0,
"error_string" : "OK.",
"message" : "OK.",
"token" : {
"app_id" : "1",
"created" : "2011-08-19 11:30:31",
"token" : "ecb8862189974248233dfcc7e8fc1e4514e16972",
"user_id" : "1"
},
"user" : {
"avatar_url" : "",
"created" : "2011-08-19 11:29:21",
"email" : "[email protected]",
"forename" : "Matthew",
"gender" : "M",
}
}
What's the correct way to map this out, we've got a class setup for User and Token, but all the examples i've seen seem to not show something like this where there is two segments to the response, here's the code we have at the moment:
// Mapping for User
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"created" toAttribute:@"created"];
[userMapping mapKeyPath:@"avatar_url" toAttribute:@"avatarURL"];
[userMapping mapKeyPath:@"gender" toAttribute:@"gender"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"];
// Mapping for Token
RKObjectMapping* tokenMapping = [RKObjectMapping mappingForClass:[Token class]];
[tokenMapping mapAttributes:@"user_id", @"app_id", @"token", @"created", nil];
[[RKObjectManager sharedManager].mappingProvider setMapping:tokenMapping forKeyPath:@"token"];
// Load the object model via RestKit
[[[RKObjectManager sharedManager] client] setValue:@"xxxxxxxxxxxxxxxxxxx" forHTTPHeaderField:@"X-API-KEY"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/users/authenticate/" delegate:self];
Appreciate any help you can give on this, loving RestKit so far!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该为此映射配置所需的一切。您可能想要使用 didLoadObjectDictionary: 委托回调,以便可以通过可映射的 keyPath 来识别对象。否则,如果您使用 didLoadObjects:,您应该得到一个带有 User & 的数组。其中的 Token 对象。
You should have everything you need configured for this mapping. You probably want to use the didLoadObjectDictionary: delegate call-back so that you can identify the objects by mappable keyPath. Otherwise if you use didLoadObjects:, you should just wind up with an Array with a User & Token object inside of it.