接收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" endBut 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]) endYou 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更多推荐
发布评论