将数据从 JSON 文件导入 R
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2617600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing data from a JSON file into R
提问by user313967
Is there a way to import data from a JSON file into R? More specifically, the file is an array of JSON objects with string fields, objects, and arrays. The RJSON Package isn't very clear on how to deal with this http://cran.r-project.org/web/packages/rjson/rjson.pdf.
有没有办法将数据从 JSON 文件导入 R?更具体地说,该文件是一个包含字符串字段、对象和数组的 JSON 对象数组。RJSON 包不太清楚如何处理这个http://cran.r-project.org/web/packages/rjson/rjson.pdf。
回答by rcs
First install the rjsonpackage:
首先安装rjson软件包:
install.packages("rjson")
Then:
然后:
library("rjson")
json_file <- "http://api.worldbank.org/country?per_page=10®ion=OED&lendingtype=LNX&format=json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
Update:since version 0.2.1
更新:从 0.2.1 版开始
json_data <- fromJSON(file=json_file)
回答by xn.
jsonlitewill import the JSON into a data frame. It can optionally flatten nested objects. Nested arrays will be data frames.
jsonlite将 JSON 导入到数据框中。它可以选择展平嵌套对象。嵌套数组将是数据框。
> library(jsonlite)
> winners <- fromJSON("winners.json", flatten=TRUE)
> colnames(winners)
[1] "winner" "votes" "startPrice" "lastVote.timestamp" "lastVote.user.name" "lastVote.user.user_id"
> winners[,c("winner","startPrice","lastVote.user.name")]
winner startPrice lastVote.user.name
1 68694999 0 Lamur
> winners[,c("votes")]
[[1]]
ts user.name user.user_id
1 Thu Mar 25 03:13:01 UTC 2010 Lamur 68694999
2 Thu Mar 25 03:13:08 UTC 2010 Lamur 68694999
回答by Karsten W.
An alternative package is RJSONIO. To convert a nested list, lapply can help:
另一个包是 RJSONIO。要转换嵌套列表,lapply 可以提供帮助:
l <- fromJSON('[{"winner":"68694999", "votes":[
{"ts":"Thu Mar 25 03:13:01 UTC 2010", "user":{"name":"Lamur","user_id":"68694999"}},
{"ts":"Thu Mar 25 03:13:08 UTC 2010", "user":{"name":"Lamur","user_id":"68694999"}}],
"lastVote":{"timestamp":1269486788526,"user":
{"name":"Lamur","user_id":"68694999"}},"startPrice":0}]'
)
m <- lapply(
l[[1]]$votes,
function(x) c(x$user['name'], x$user['user_id'], x['ts'])
)
m <- do.call(rbind, m)
gives information on the votes in your example.
在您的示例中提供有关投票的信息。
回答by Anthony
If the URL is https, like used for Amazon S3, then use getURL
如果 URL 是 https,例如用于 Amazon S3,则使用 getURL
json <- fromJSON(getURL('https://s3.amazonaws.com/bucket/my.json'))
回答by Moby M
First install the RJSONIO and RCurl package:
首先安装 RJSONIO 和 RCurl 包:
install.packages("RJSONIO")
install.packages("(RCurl")
Try below code using RJSONIO in console
在控制台中使用 RJSONIO 尝试以下代码
library(RJSONIO)
library(RCurl)
json_file = getURL("https://raw.githubusercontent.com/isrini/SI_IS607/master/books.json")
json_file2 = RJSONIO::fromJSON(json_file)
head(json_file2)
回答by Aaron C
packages:
包:
- library(httr)
- library(jsonlite)
- 图书馆(httr)
- 图书馆(jsonlite)
I have had issues converting json to dataframe/csv. For my case I did:
我在将 json 转换为 dataframe/csv 时遇到了问题。对于我的情况,我做了:
Token <- "245432532532"
source <- "http://......."
header_type <- "applcation/json"
full_token <- paste0("Bearer ", Token)
response <- GET(n_source, add_headers(Authorization = full_token, Accept = h_type), timeout(120), verbose())
text_json <- content(response, type = 'text', encoding = "UTF-8")
jfile <- fromJSON(text_json)
df <- as.data.frame(jfile)
then from df to csv.
然后从 df 到 csv。
In this format it should be easy to convert it to multiple .csvs if needed.
在这种格式中,如果需要,应该很容易将其转换为多个 .csvs。
The important part is content function should have type = 'text'.
重要的部分是内容功能应该具备的type = 'text'。
回答by Adarsh Pawar
import httr package
导入 httr 包
library(httr)
Get the url
获取网址
url <- "http://www.omdbapi.com/?apikey=72bc447a&t=Annie+Hall&y=&plot=short&r=json"
resp <- GET(url)
Print content of resp as text
将 resp 的内容打印为文本
content(resp, as = "text")
Print content of resp
打印响应内容
content(resp)
Use content() to get the content of resp, but this time do not specify a second argument. R figures out automatically that you're dealing with a JSON, and converts the JSON to a named R list.
使用 content() 获取 resp 的内容,但这次不指定第二个参数。R 会自动判断您正在处理 JSON,并将 JSON 转换为命名的 R 列表。

