0

我是 R 新手,我正在练习将 JSON 转换为 R,然后转换为 CSV 或 Excel。我从https://mtgjson.com/下载了整个 Magic the Gathering 卡集进行练习。我使用此代码将其放入“列表”中,但我希望将其放入数据框或表格中。

   json_file<- rjson::fromJSON(file= "C://Users/ahalman/Desktop/AllCards.json")

我试过了:

   as.data.frame(json_file)

但我收到一条错误消息:“错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数暗示不同的行数:13、10、14 , 15, 11, 12, 16, 19, 9, 8, 6, 7, 18, 4, 17, 21, 23, 20, 22"

我认为这会解决它,正如我在另一个 Stack Overflow 页面上看到的那样,但不管它做什么,它都不起作用:

  json_file <- lapply(json_file, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
  })

任何帮助都会很棒。一旦它采用数据框格式,我就明白了!

4

1 回答 1

1

我相信这是答案:

  df<- stringi::stri_list2matrix(json_file, byrow = TRUE)

这适用于前几列,但事情开始变得混乱。不知道为什么。

我想我终于弄明白了:

 newdf<- stringi::stri_list2matrix(json_file, byrow = TRUE, fill = "")
于 2017-07-17T21:05:08.617 回答