【发布时间】:2018-10-19 11:59:25
【问题描述】:
我目前正在构建一个包含多层信息的 api。目前我正在使用小标题并将信息嵌套在列下:
# A tibble: 1 x 3
APICallTimeGMT Error some_data
<dttm> <list> <list>
1 2018-10-19 11:43:10 <data.frame [1 × 2]> <tibble [2 × 3]>
当我使用jsonlite::toJSON 将其转换为JSON 时,我会得到很多列表[] 括号。有没有办法将这些特定对象转换为 json 对象而不是列表。那么,嵌套对象应该是什么类(而不是 data.frame)?
当前输出:
期望的输出:
这里是dput(x) 用于重现性:
structure(list(APICallTimeGMT = structure(1539949390.26164, class = c("POSIXct",
"POSIXt")), Error = list(structure(list(msg = structure(1L, .Label = "OK", class = "factor"),
status = 200), .Names = c("msg", "status"), row.names = c(NA,
-1L), class = "data.frame")), some_data = list(structure(list(
Outcome = c("Case1", "Case2"), predictions = list(structure(list(
ClassA = 0.4, ClassB = 0.1, ClassC = 0.5), .Names = c("ClassA",
"ClassB", "ClassC"), row.names = c(NA, -1L), class = "data.frame"),
structure(list(ClassA = 0.4, ClassB = 0.1, ClassC = 0.5), .Names = c("ClassA",
"ClassB", "ClassC"), row.names = c(NA, -1L), class = "data.frame")),
meta = list(structure(list(model = structure(1L, .Label = "Awesome AI", class = "factor"),
runs = 55), .Names = c("model", "runs"), row.names = c(NA,
-1L), class = "data.frame"), structure(list(model = structure(1L, .Label = "Awesome AI", class = "factor"),
runs = 55), .Names = c("model", "runs"), row.names = c(NA,
-1L), class = "data.frame"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("Outcome", "predictions", "meta"
)))), row.names = c(NA, -1L), .Names = c("APICallTimeGMT", "Error",
"some_data"), class = c("tbl_df", "tbl", "data.frame"))
【问题讨论】: