【问题标题】:R data frame to custom json formatR数据框到自定义json格式
【发布时间】:2017-01-11 17:33:36
【问题描述】:

我的项目中有一个 R 数据框,我想将其转换为 json 格式。

我的数据框是“df_sales”:

df_sales
         2    4      5        7        8         9        10     11     12
2004 40400    0 226100 238556.4 217044.8 2243085.4 118754.50 193740 181075
2005     0    0      0      0.0  60000.0       0.0      0.00      0      0
2006     0    0      0      0.0      0.0       0.0      4.75      0      0
2007     0 8100      0 162400.0   1500.0  181860.9    450.00      0  73100
2008     0    0      0      0.0  10000.0    1000.0      0.00      0      0

我的 df_sales 数据框的 dput 输出:

dput(df_sales)
structure(list(`2` = c(40400, 0, 0, 0, 0), `4` = c(0, 0, 0, 8100, 
0), `5` = c(226100, 0, 0, 0, 0), `7` = c(238556.35, 0, 0, 162400, 
0), `8` = c(217044.75, 60000, 0, 1500, 10000), `9` = c(2243085.4, 
0, 0, 181860.85, 1000), `10` = c(118754.5, 0, 4.75, 450, 0), 
    `11` = c(193740, 0, 0, 0, 0), `12` = c(181075, 0, 0, 73100, 
    0)), .Names = c("2", "4", "5", "7", "8", "9", "10", "11", 
"12"), row.names = c("2004", "2005", "2006", "2007", "2008"), class = "data.frame")

我想将我的 df_sales 数据框转换为自定义 json 格式:

{
   "series":[
      {
         "year":"2004",
         "month":{
            "2":40400,
            "4":0,
            "5":226100,
            "7":238556.35,
            "8":217044.75,
            "9":2243085.4,
            "10":118754.5,
            "11":193740,
            "12":181075
         }
      },
      {
         "year":"2005",
         "month":{
            "2":0,
            "4":0,
            "5":0,
            "7":0,
            "8":60000,
            "9":0,
            "10":0,
            "11":0,
            "12":0
         }
      },
      {
         "year":"2006",
         "month":{
            "2":0,
            "4":0,
            "5":0,
            "7":0,
            "8":0,
            "9":0,
            "10":4.75,
            "11":0,
            "12":0
         }
      },
      {
         "year":"2007",
         "month":{
            "2":0,
            "4":8100,
            "5":0,
            "7":162400,
            "8":1500,
            "9":181860.85,
            "10":450,
            "11":0,
            "12":73100
         }
      },
      {
         "year":"2008",
         "month":{
            "2":0,
            "4":0,
            "5":0,
            "7":0,
            "8":10000,
            "9":1000,
            "10":0,
            "11":0,
            "12":0
         }
      }
   ]
}

我使用 jsonlite 包和 rjson 来转换我的“df_sales”,但我无法使用这些包获得这种结构。

我尝试使用 R apply()、lapply() 和 sapply() 函数来获取我的自定义 json:

toJSON(list(series=list(df_sales),MARGIN=1,FUN=function(r){
sapply(r,FUN=function(i){list(i)})
}))), pretty = TRUE, auto_unbox = TRUE)

但我只是得到这个结果:

{
  "series": [
    {
      "2004": {
        "2": 40400,
        "4": 0,
        "5": 226100,
        "7": 238556.35,
        "8": 217044.75,
        "9": 2243085.4,
        "10": 118754.5,
        "11": 193740,
        "12": 181075
      },
      "2005": {
        "2": 0,
        "4": 0,
        "5": 0,
        "7": 0,
        "8": 60000,
        "9": 0,
        "10": 0,
        "11": 0,
        "12": 0
      },
      "2006": {
        "2": 0,
        "4": 0,
        "5": 0,
        "7": 0,
        "8": 0,
        "9": 0,
        "10": 4.75,
        "11": 0,
        "12": 0
      },
      "2007": {
        "2": 0,
        "4": 8100,
        "5": 0,
        "7": 162400,
        "8": 1500,
        "9": 181860.85,
        "10": 450,
        "11": 0,
        "12": 73100
      },
      "2008": {
        "2": 0,
        "4": 0,
        "5": 0,
        "7": 0,
        "8": 10000,
        "9": 1000,
        "10": 0,
        "11": 0,
        "12": 0
      }
    }
  ]
} 

您有什么建议或其他解决方案吗?

谢谢。

【问题讨论】:

  • 如果您可以在数据框中提供dput 的输出,我们就不必重新构建它,我们可以将dput 输出粘贴进去。
  • 我很确定您想要的结果不是有效的 JSON。您有一些拼写错误(在每年年末编号时缺少 "),但更严重的是,您有重复的键,"year""month" 都作为键名重复,这是不允许的。您得到的输出 有效的 JSON,但我认为修改 jsonlite 以产生您想要的非法结果会有点困难。
  • 谢谢,我错过了我的复制/粘贴。我刚刚在我的帖子中更改了自定义 json 格式。

标签: json r jsonlite rjson


【解决方案1】:

这是一种方法:

toJSON(
  list(
    series =
      lapply(row.names(df_sales), function(x) {
        list(
          year = x,
          month = as.list(df_sales[x,])
        )
      })    
  ),
  auto_unbox = TRUE,
  pretty = TRUE
)

注意:

  • 从 row.names 中读取年份
  • 每一行都转换为一个列表
  • 使用 auto_unbox 选项

结果:

{
  "series": [
    {
      "year": "2004",
      "month": {
        "2": 40400,
        "4": 0,
        "5": 226100,
        "7": 238556.35,
        "8": 217044.75,
        "9": 2243085.4,
        "10": 118754.5,
        "11": 193740,
        "12": 181075
      }
    },
    {
      "year": "2005",
      "month": {
        "2": 0,
        "4": 0,
        "5": 0,
        "7": 0,
        "8": 60000,
        "9": 0,
        "10": 0,
        "11": 0,
        "12": 0
      }
    },
    {
      "year": "2006",
      "month": {
        "2": 0,
        "4": 0,
        "5": 0,
        "7": 0,
        "8": 0,
        "9": 0,
        "10": 4.75,
        "11": 0,
        "12": 0
      }
    },
    {
      "year": "2007",
      "month": {
        "2": 0,
        "4": 8100,
        "5": 0,
        "7": 162400,
        "8": 1500,
        "9": 181860.85,
        "10": 450,
        "11": 0,
        "12": 73100
      }
    },
    {
      "year": "2008",
      "month": {
        "2": 0,
        "4": 0,
        "5": 0,
        "7": 0,
        "8": 10000,
        "9": 1000,
        "10": 0,
        "11": 0,
        "12": 0
      }
    }
  ]
} 

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多