接收API调用轨中的哈希数组(receive Array of hashes in API call rails)

编程入门 行业动态 更新时间:2024-10-24 04:34:17
接收API调用轨中的哈希数组(receive Array of hashes in API call rails)

我正在使用rails app,我需要在Grape之类的API调用中接收哈希数组。

{ tournament_id:1 match_id: 10 [ { team_id: 1 score: 10 }, { team_id: 2 score: 20 } ] }

这样我就可以通过一次通话获得每支球队的得分,从而获得特定的比赛和锦标赛,而不是每支球队得分多次。

我尝试过多种方式

group :teams_with_scores, type: Array, desc: "An array of Teams with scores" do requires :team_id, type: String,desc: "Team ID" requires :score, type: String,desc: "Score" end

但是不知道该怎么做。

I 'm working on rails app in which I need to receive array of hashes in API call with Grape like.

{ tournament_id:1 match_id: 10 [ { team_id: 1 score: 10 }, { team_id: 2 score: 20 } ] }

so that I can receive score of each team in single call for a specific match and tournament instead of multiple calls for score of each team.

I have tried multiple things like

group :teams_with_scores, type: Array, desc: "An array of Teams with scores" do requires :team_id, type: String,desc: "Team ID" requires :score, type: String,desc: "Score" end

But don't have a clue that how to do it.

最满意答案

您可以将此数据作为json字符串发送,然后在获取时解析此json:

params do scores_info, type: String, desc: 'the scores info' end get do scores_info = JSON.parse(params[:scores_info]) end

You can send this data as a json string, and then parse this json when you get it:

params do scores_info, type: String, desc: 'the scores info' end get do scores_info = JSON.parse(params[:scores_info]) end

更多推荐

本文发布于:2023-07-29 20:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319553.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   receive   API   Array   rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!