从 R 中的 JSON 文件中提取数据
Extract data from JSON file in R
我有一个 .json 文件,其中包含相应的 ID 和 类别名称 对。我怎样才能提取它们?
我转换了文件:
categories <- fromJSON("CA_category_id.json")
结果得到一个嵌套列表。
我需要:
categories[["items"]]["id"]
和
categories[["items"]][["snippet"]][["title"]]
合二为一table。像这样:
id
title
1
Film & Animation
2
Autos & Vehicles
10
Music
15
Pets & Animals
...
...
您可以找到初始的 .json 文件 here。它被称为CA_category_id.json.
感谢您的帮助。
一个简单的方法是逐项进行。工作代码如下:
library(rjson)
library(data.table)
categories <- fromJSON(file="CA_category_id.json")
table <- NULL
for (index in seq_along(categories$items) ) {
table <- rbind(table,data.table(id=categories[["items"]][[index]][["id"]],title=categories[["items"]][[index]][["snippet"]][["title"]]))
}
table
我有一个 .json 文件,其中包含相应的 ID 和 类别名称 对。我怎样才能提取它们?
我转换了文件:
categories <- fromJSON("CA_category_id.json")
结果得到一个嵌套列表。
我需要:
categories[["items"]]["id"]
和
categories[["items"]][["snippet"]][["title"]]
合二为一table。像这样:
id | title |
---|---|
1 | Film & Animation |
2 | Autos & Vehicles |
10 | Music |
15 | Pets & Animals |
... | ... |
您可以找到初始的 .json 文件 here。它被称为CA_category_id.json.
感谢您的帮助。
一个简单的方法是逐项进行。工作代码如下:
library(rjson)
library(data.table)
categories <- fromJSON(file="CA_category_id.json")
table <- NULL
for (index in seq_along(categories$items) ) {
table <- rbind(table,data.table(id=categories[["items"]][[index]][["id"]],title=categories[["items"]][[index]][["snippet"]][["title"]]))
}
table