使用 Restkit RKParams 发送 JSON 请求

发布于 2024-11-17 22:53:19 字数 617 浏览 4 评论 0原文

我需要向 REST 服务发送 JSON 请求。我正在使用 Restkit RKParams 发送请求。

目前它的工作原理如下:

[params setValue:@"-46.566393" forParam:@"checkin[lng]"]; 
[params setValue:@"-23.541576" forParam:@"checkin[lat]"];

发送:

{
    "checkin":
    {
        "lng":"-26.566393",
        "lat":"-63.541576"
    }
}

现在我想形成这样的 JSON 数据(还有更多项目):

{
    "checkin":
    {
        "lng":"-26.566393",
        "lat":"-63.541576",
        "votes":
        [
            {"vote_id":28},
            {"vote_id":11}
        ]
    }
}

我将如何设置参数以按需要工作?有可能这样做吗?

I need to send a JSON request to a REST service. I'm using Restkit RKParams to send a request.

Currently it works as follows:

[params setValue:@"-46.566393" forParam:@"checkin[lng]"]; 
[params setValue:@"-23.541576" forParam:@"checkin[lat]"];

Send:

{
    "checkin":
    {
        "lng":"-26.566393",
        "lat":"-63.541576"
    }
}

Now I want to form JSON data like this (with few more items):

{
    "checkin":
    {
        "lng":"-26.566393",
        "lat":"-63.541576",
        "votes":
        [
            {"vote_id":28},
            {"vote_id":11}
        ]
    }
}

How would I set the params to work as desired? It is possible to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

月亮邮递员 2024-11-24 22:53:19

RestKit 确实处理嵌套模型,但看起来它并没有直接在 RKParams 中执行此操作。我会看看其他一些课程来完成您想做的事情。 键值映射 看起来它会做你想要的。

或者如果你想破解它,

[RKParams setValue:@"[{\"vote_id\":28},{\"vote_id\":11}]" forParam:@"checkin[votes]"]

也许可行。但没有任何承诺。

RestKit does handle nested models, but it looks like it doesn't do that directly in RKParams. I would look at some of the other classes to do what you're trying to do. Key-value mapping looks like it will do what you want.

Or if you want to hack it,

[RKParams setValue:@"[{\"vote_id\":28},{\"vote_id\":11}]" forParam:@"checkin[votes]"]

might just work. No promises, though.

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