如何使用嵌套列表构造JSON

时间:2017-03-04 03:10:30

标签: json r jsonlite

我需要动态构建以下JSON。

{
  "status": "SUCCESS",
  "code": "200",
  "output": {
    "studentid": "1001",
    "name": "Kevin"
  }
}

我尝试使用 jsonlite 包,但我无法构建内部JSON对象。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:10)

如评论中所述,您可以创建嵌套列表并使用toJSON()

library(jsonlite)
x <- list(status = "SUCCESS", code = "200", 
    output = list(studentid = "1001", name = "Kevin"))
toJSON(x, pretty = TRUE, auto_unbox = TRUE)

给出以下输出:

{
  "status": "SUCCESS",
  "code": "200",
  "output": {
    "studentid": "1001",
    "name": "Kevin"
  }
}