使用 jsonlite 解析嵌套列表

Nested List Parsing with jsonlite

这是我最近第二次遇到这个问题,所以我想看看是否有更好的方法来解析从 jsonlite 返回的数据帧,其中一个元素是存储为的数组数据框中的一列作为列表。

我知道这部分功能与 jsonlite 有关,但我不确定如何使用这种嵌套结构。最后,我想我可以编写自己的自定义解析,但考虑到我快完成了,我想看看如何使用这些数据。

例如:

## options
options(stringsAsFactors=F)

## packages
library(httr)
library(jsonlite)

## setup
gameid="2015020759"
SEASON = '20152016'
BASE = "http://live.nhl.com/GameData/"
URL = paste0(BASE, SEASON, "/", gameid, "/PlayByPlay.json")

## get the data
x <- GET(URL)

## parse
api_response <- content(x, as="text")
api_response <- jsonlite::fromJSON(api_response, flatten=TRUE)

## get the data of interest
pbp <- api_response$data$game$plays$play
colnames(pbp)

探索返回的内容:

> class(pbp$aoi)
[1] "list"
> class(pbp$desc)
[1] "character"
> class(pbp$xcoord)
[1] "integer"

从上面看,pbp$aoi 列是一个列表。以下是一些条目:

> head(pbp$aoi)
[[1]]
[1] 8465009 8470638 8471695 8473419 8475792 8475902

[[2]]
[1] 8470626 8471276 8471695 8476525 8476792 8477956

[[3]]
[1] 8469619 8471695 8473492 8474625 8475727 8476525

[[4]]
[1] 8469619 8471695 8473492 8474625 8475727 8476525

[[5]]
[1] 8469619 8471695 8473492 8474625 8475727 8476525

[[6]]
[1] 8469619 8471695 8473492 8474625 8475727 8475902

我真的不在乎我是否在同一个数据框中解析这些列表,但是我有什么选项可以解析数据?

我更愿意从列表中取出数据并将它们解析成一个数据帧,该数据帧可以 "related" 到它来自的原始记录。

在此先感谢您的帮助。

从上面的@hrbmstr,我可以使用 unnest.

得到我想要的东西
select(pbp, eventid, aoi) %>% unnest() %>% head