R数据帧为自定义json格式

时间:2017-01-11 17:33:36

标签: json r jsonlite rjson

我的项目中有一个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(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
      }
    }
  ]
} 

您有任何建议或任何其他解决方案吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

这是一种方法:

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
      }
    }
  ]
}