您的代码似乎过于复杂。人们很容易被列表树搞糊涂。请不要尝试处理整个
purList1
同时播放子集,如
purList1[[1]]
第一
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-11-18
df <- data.frame( sec = c('15:31:36',"15:31:37",'15:31:37','15:31:37','15:31:38','15:31:39'),
label = c("Choose to and fro flights","Choose to and fro flights","Details","Details","Details","Details"),
responseCode = c(200,200,200,'Non HTTP response code: org.apache.http.NoHttpResponseException','200','200'),
Counting = c(9,1,2,2,5,1))
purList1 <- lapply(split(df, df$label), function(x) split(x, x$responseCode,drop = T))
# play with a subset and construct the function to apply
jsonlite::toJSON(
lapply(
purList1[[1]],
function(x) list(
R_C_seconds = x$sec,
R_C_Count = x$Counting
)
),
pretty = TRUE
)
#> {
#> "200": {
#> "R_C_seconds": ["15:31:36", "15:31:37"],
#> "R_C_Count": [9, 1]
#> }
#> }
# apply the function on the whole list
jsonlite::toJSON(
lapply(
purList1,
lapply,
function(x) list(
R_C_seconds = x$sec,
R_C_Count = x$Counting
)
),
pretty = TRUE
)
#> {
#> "Choose to and fro flights": {
#> "200": {
#> "R_C_seconds": ["15:31:36", "15:31:37"],
#> "R_C_Count": [9, 1]
#> }
#> },
#> "Details": {
#> "200": {
#> "R_C_seconds": ["15:31:37", "15:31:38", "15:31:39"],
#> "R_C_Count": [2, 5, 1]
#> },
#> "Non HTTP response code: org.apache.http.NoHttpResponseException": {
#> "R_C_seconds": ["15:31:37"],
#> "R_C_Count": [2]
#> }
#> }
#> }
(我不确定,但是
jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE)
如果您只想转换数据,可能就足够了。柱方向的框架)