在 API 调用 rails 中接收哈希数组
receive Array of hashes in API call rails
我正在开发 rails 应用程序,我需要在 API 调用中接收散列数组,如 Grape。
{
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
但不知道如何去做。
您可以将此数据作为 json 字符串发送,然后在收到后解析此 json:
params do
scores_info, type: String, desc: 'the scores info'
end
get do
scores_info = JSON.parse(params[:scores_info])
end
我正在开发 rails 应用程序,我需要在 API 调用中接收散列数组,如 Grape。
{
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
但不知道如何去做。
您可以将此数据作为 json 字符串发送,然后在收到后解析此 json:
params do
scores_info, type: String, desc: 'the scores info'
end
get do
scores_info = JSON.parse(params[:scores_info])
end