我需要导出到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"]}
}
最佳答案 您可以使用其他级别的列表来获取该结构:
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"]
#> }
#> ]
#> }