我需要将列表列表导出到json,以获得下一个输出的JSON:
{
"timestamp":"2018-02-01 10:20",
"aux":[
{"id":"x1","prog":['A','A','A']},
{"id":"x2","prog":['A','A','A']},
{"id":"x3","prog":['A','A','A']}
]
}
如何创建一个列表以获取类似的JSON?预先感谢。
编辑:
这就是我正在尝试的:
l1 <- list(timestamp = "2018-02-01 10:20",
aux = list(id = c("x1","x2","x3"),
prog = rep('A',3)))
这是我得到的JSON:
{"timestamp":"2018-02-01 10:20",
"aux":{"id":["x1","x2","x3"],
"prog":["A","A","A"]}
}
答案 0 :(得分:1)
您可以通过附加级别的列表获得该结构:
l1 <- list(timestamp = "2018-02-01 10:20",
aux = list(list(id = "x1", prog = rep('A',3)),
list(id = "x2", prog = rep('A',3)),
list(id = "x3", prog = rep('A',3))))
jsonlite::toJSON(l1, pretty = TRUE, auto_unbox = TRUE)
#> {
#> "timestamp": "2018-02-01 10:20",
#> "aux": [
#> {
#> "id": "x1",
#> "prog": ["A", "A", "A"]
#> },
#> {
#> "id": "x2",
#> "prog": ["A", "A", "A"]
#> },
#> {
#> "id": "x3",
#> "prog": ["A", "A", "A"]
#> }
#> ]
#> }