首页 > 解决方案 > 如何将 JSON 转换为 DF?

问题描述

我正在使用 Rstudio 代码,我有 450 个 JSON 文件,我的工作区中都有,一些 JSON 文件都是正确的,但是有一些像这样的文件(https://drive.google.com/file/d /1DsezCmN8_8iLNCAsLZiRnxTrwnWu6LkD/view?usp=sharing,是 296kb json)当我尝试将字段 tm 设置为数据帧时,我遇到了这个错误

(function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : 参数暗示不同的行数: 0, 1

我使用的代码是

JSONList <- rjson::fromJSON(file = "2.json", simplify = F)

DF <- as.data.frame(JSONList$tm)

使用正常的文件,我获得了 5168 个变量的 1 个观察值。

我怎样才能避免一些文件的这个问题?

谢谢

标签: rjson

解决方案


我认为另一种可能性是选择我需要的行

    candidatos = list(
"name",
"score",
"tot_sFieldGoalsMade",
"tot_sFieldGoalsAttempted",
"tot_sTwoPointersMade",
"tot_sTwoPointersAttempted",
"tot_sThreePointersMade",
"tot_sThreePointersAttempted",
"tot_sFreeThrowsMade",
"tot_sFreeThrowsAttempted",
"tot_sReboundsDefensive",
"tot_sReboundsOffensive",
"tot_sReboundsTotal",
"tot_sAssists",
"tot_sBlocks",
"tot_sTurnovers",
"tot_sFoulsPersonal",
"tot_sPointsInThePaint",
"tot_sPointsSecondChance",
"tot_sPointsFromTurnovers",
"tot_sBenchPoints",
"tot_sPointsFastBreak",
"tot_sSteals"
)

 ListColum<-map(candidatos, function(x){
    as.data.frame(data$tm$"2"$x)
     } )

但是 R 给了我一个没有元素的 23 个 DF 的列表


推荐阅读