【发布时间】:2019-02-24 18:39:36
【问题描述】:
我正在尝试使用类似于下面的嵌套 JSON 格式。我最终需要的是数据框只有两行数据,一行给 John,一行给 Sam,其他数据格式如下。所以这个特定的数据框将有 2 行和 7 列。
Name RD1 RD2 Hours1 Hours2 Billable1 Billable2
John
Sam
如何做到这一点?提前谢谢!
代码如下:
library(jsonlite)
options(stringsAsFactors = FALSE)
rawData <- "document.txt"
processedData <- fromJSON(rawData, flatten = TRUE)
processedData <- processedData[, c("name", "records")]
unnestedJSON <- unnest(processedData, records)
document.txt 包含以下信息:
[
{
"name": "John",
"records": [
{
"reportDate": "2018-07-20",
"hours": 204,
"billable": 32844
},
{
"reportDate": "2018-03-25",
"hours": 234,
"billable": 37715
}
]
},
{
"name": "Sam",
"records": [
{
"reportDate": "2018-06-18",
"hours": 187,
"billable": 13883
},
{
"reportDate": "2018-04-02",
"hours": 176,
"billable": 13467
}
]
}
]
【问题讨论】: