RestKit - POST 响应对象映射

发布于 2024-12-06 10:50:20 字数 989 浏览 1 评论 0原文

我的 RESTful web 服务(Rails)上有方法“getBuses”。该方法需要返回 我的 JSON 格式的公交车列表。但是,当我发送“/getBuses.json”请求时,我 需要发送

params = [NSDictionary 
dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", 
@"fromSerbia", nil]; 

我正在发布参数,因为我需要过滤响应(仅针对特定 特定日期的游览)。 然后,我期待这样的响应:

{ 
  "buses" : [ { 
    "bus_number" : "1", 
    "created_at" : "2011-08-15T23:07:52Z", 
    "id" : 1, 
    "lat" : 44.815, 
    "long" : 20.4665, 
    "model" : "Setra", 
    "registar_number" : "123456", 
    "seats" : 50, 
    "tour_id" : 1, 
    "updated_at" : "2011-08-15T23:07:52Z" 
  }, { 
    "bus_number" : "2", 
    "created_at" : "2011-08-15T23:07:52Z", 
    "id" : 2, 
    "lat" : 44.812, 
    "long" : 20.465, 
    "model" : "Mercedes", 
    "registar_number" : "2234", 
    "seats" : 60, 
    "tour_id" : 1, 
    "updated_at" : "2011-08-15T23:07:52Z" 
  } ] 
} 

我只需要带有 Bus 对象的 NSArray 。 你能给我代码示例如何发出此请求并使用 RestKit 进行对象映射吗?

I have method "getBuses" on my RESTful webservice (Rails). That method needs to return
me list of buses in JSON. But, when I sending request for "/getBuses.json" I
need to send

params = [NSDictionary 
dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", 
@"fromSerbia", nil]; 

I'm posting params because I need filtered response (just for specific
tour on specific date).
Then, I expecting response like this:

{ 
  "buses" : [ { 
    "bus_number" : "1", 
    "created_at" : "2011-08-15T23:07:52Z", 
    "id" : 1, 
    "lat" : 44.815, 
    "long" : 20.4665, 
    "model" : "Setra", 
    "registar_number" : "123456", 
    "seats" : 50, 
    "tour_id" : 1, 
    "updated_at" : "2011-08-15T23:07:52Z" 
  }, { 
    "bus_number" : "2", 
    "created_at" : "2011-08-15T23:07:52Z", 
    "id" : 2, 
    "lat" : 44.812, 
    "long" : 20.465, 
    "model" : "Mercedes", 
    "registar_number" : "2234", 
    "seats" : 60, 
    "tour_id" : 1, 
    "updated_at" : "2011-08-15T23:07:52Z" 
  } ] 
} 

I just need NSArray with Bus objects.
Can you give me code example how to make this request and make object mapping with RestKit?

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

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

发布评论

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

评论(1

荒芜了季节 2024-12-13 10:50:20

正如我在 IRC 频道中告诉您的,在 RKClient 实例中,您可以使用以下方法:

(NSURL *)URLForResourcePath:(NSString *)resourcePath queryParams:(NSDictionary *)queryParams

所以您应该这样做:

[objectManager.mappingProvider setMapping:busesMapping forKeyPath:@"buses"];

params = [NSDictionary dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", @"fromSerbia", nil];
NSURL *someURL = [objectManager.client URLForResourcePath:@"/service/getLocations.json" queryParams:params];

[objectManager loadObjectsAtResourcePath:[someURL absoluteString] delegate:self];

该方法构建一个有效的使用资源路径和您作为参数提供的参数的 URL。然后您需要使用 NSURL 对象的absoluteString 将其设置回来。希望这有帮助!

As I told you in the IRC Channel, in your RKClient instance you can use this method:

(NSURL *)URLForResourcePath:(NSString *)resourcePath queryParams:(NSDictionary *)queryParams

So you should do it like this:

[objectManager.mappingProvider setMapping:busesMapping forKeyPath:@"buses"];

params = [NSDictionary dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", @"fromSerbia", nil];
NSURL *someURL = [objectManager.client URLForResourcePath:@"/service/getLocations.json" queryParams:params];

[objectManager loadObjectsAtResourcePath:[someURL absoluteString] delegate:self];

That method builds a valid URL using the resourcePath and the parameters you give as parameter. And then you need to set it back using the absoluteString of the NSURL object. Hope this helps!

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