有没有办法将数据从JSON文件导入到R中?更具体地说,该文件是一个JSON对象数组,其中包含字符串字段、对象和数组。RJSON包对于如何处理这个http://cran.r-project.org/web/packages/rjson/rjson.pdf并不是很清楚。
发布于 2014-09-05 05:51:33
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
发布于 2010-04-12 03:18:20
另一个包是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)
提供了有关您示例中的投票的信息。
发布于 2013-06-10 12:44:10
如果网址是https,如用于亚马逊S3,则使用getURL
json <- fromJSON(getURL('https://s3.amazonaws.com/bucket/my.json'))
https://stackoverflow.com/questions/2617600
复制相似问题