R 将名称列为JSON对象名称
我在R中面临JSON对象命名的问题R 将名称列为JSON对象名称,r,dplyr,jsonlite,R,Dplyr,Jsonlite,我在R中面临JSON对象命名的问题 df = list(`Choose to and fro flights` = data.frame(sec =c('15:31:36' ,'15:31:37'),label=c("Choose to and fro flights","Choose to and fro flights"),responseCode = c( 200, 200), Counting=c(9,1)), `Details` = data.frame(sec = c("15:
df = list(`Choose to and fro flights` = data.frame(sec =c('15:31:36' ,'15:31:37'),label=c("Choose to and fro flights","Choose to and fro flights"),responseCode = c( 200, 200), Counting=c(9,1)),
`Details` = data.frame(sec = c("15:31:37" ,"15:31:37","15:31:38","15:31:39"),label = c("Details","Details","Details","Details"),responseCode = c("200","Non HTTP response code: org.apache.http.NoHttpResponseException","200","200"), Counting=c(2,2,5,1)))
我希望标签名与这里的JSON对象名相同
我正在尝试以下代码,但它没有给出正确的答案
toJSON(list(ResponseCode=lapply(seq_along(df), function(i) { list(R_C_seconds=df[[i]]$sec,R_C_labels=df[[i]]$label,R_C_responseCode=df[[i]]$responseCode,R_C_Count=df[[i]]$Counting)})))
是我的输出:
我想用标签名替换0和1。lapply()
如果原始参数有名称,则在结果上设置名称。因此,您需要构建这样一个命名向量,而不是seq_-along(df)
:
idx <- seq_along(df)
names(idx) <- names(df)
lappy()
如果原始参数有名称,则在结果上设置名称。因此,您需要构建这样一个命名向量,而不是seq_-along(df)
:
idx <- seq_along(df)
names(idx) <- names(df)