使用以下 JSON 响应映射 RestKit 中的对象的正确方法是什么

发布于 2024-12-01 11:21:40 字数 1810 浏览 1 评论 0原文

这个问题有 2 个部分,都与 RestKit 相关:

  1. 我们如何发布 2 个值电子邮件和密码,并使用对象映射器处理响应
  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:

  1. How can we post up 2 values email and password, and deal with the response using the object mapper
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2024-12-08 11:21:40

您应该为此映射配置所需的一切。您可能想要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文