0

我的目标是实现管道工 R HTTP 包的响应。

library(rjson)

formatResponse = function(matchId, home, draw, away) {
  return(toJSON(???))
}

formatResponse('myId', 10, 20, 70);

我的目标是得到:

    {
      matchId: 'myId',
      probabilities: {
        home: 10,
        draw: 20,
        away: 70
      }
    }
4

1 回答 1

2

直截了当的方法:创建一个列表并将该列表转换为 json 对象(不是最漂亮的解决方案,但有效):

formatResponse = function(matchId, home, draw, away) {
    library(rjson)
    foo <- list(matchId = matchId, 
                propabilities = list(home = home, 
                                     draw = draw, 
                                     away = away))
    toJSON(foo)
}
formatResponse("myId", 10, 20, 70)

[1] "{\"matchId\":\"myId\",\"propabilities\":{\"home\":10,\"draw\":20,\"away\":70}}"
于 2018-03-03T16:06:49.537 回答