【问题标题】:List names as JSON object name将名称列为 JSON 对象名称
【发布时间】:2017-11-14 12:18:59
【问题描述】:

我正面临 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: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)})))

是我的输出:

我想用标签名称替换 01

【问题讨论】:

    标签: r dplyr jsonlite


    【解决方案1】:

    lapply() 如果原始参数有名称,则在结果上设置名称。因此,您需要像这样构建一个命名向量,而不是 seq_along(df)

    idx <- seq_along(df)
    names(idx) <- names(df)
    

    或者您可以将df 本身传递给lapply()


    reprex::reprex_info()
    #> Created by the reprex package v0.1.1.9000 on 2017-11-14
    
    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))
    )
    
    
    jsonlite::toJSON(
      list(
        ResponseCode = lapply(df, function(x) {
          list(
            R_C_seconds      = x$sec,
            R_C_labels       = x$label,
            R_C_responseCode = x$responseCode,
            R_C_Count        = x$Counting
          )
        })
      ),
      pretty = TRUE
    )
    #> {
    #>   "ResponseCode": {
    #>     "Choose to and fro flights": {
    #>       "R_C_seconds": ["15:31:36", "15:31:37"],
    #>       "R_C_labels": ["Choose to and fro flights", "Choose to and fro flights"],
    #>       "R_C_responseCode": [200, 200],
    #>       "R_C_Count": [9, 1]
    #>     },
    #>     "Details": {
    #>       "R_C_seconds": ["15:31:37", "15:31:37", "15:31:38", "15:31:39"],
    #>       "R_C_labels": ["Details", "Details", "Details", "Details"],
    #>       "R_C_responseCode": ["200", "Non HTTP response code: org.apache.http.NoHttpResponseException", "200", "200"],
    #>       "R_C_Count": [2, 2, 5, 1]
    #>     }
    #>   }
    #> }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多