简体   繁体   English

将URL中的JSON文件导入R

[英]Import JSON file from URL into R

I've been trying to import a JSON file into R for a while using both the rjson and RJSONIO package, but I can't get it to work. 我一直在尝试使用rjson和RJSONIO包将RSON文件导入R一段时间,但我无法让它工作。 One of the variations of code I used: 我使用的代码变体之一:

json_file <- "http://toolserver.org/~emw/index.php?c=rawdata&m=get_traffic_data&p1=USA&project1=en&from=12/10/2007&to=4/1/2011"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

This results in an error message: 这会导致错误消息:

Error in fromJSON(paste(readLines(json_file), collapse = "")) : 
unexpected character '<'

I think the problem is in the first line of code, because json_file contains the source of the website, not the actual content. 我认为问题出在第一行代码中,因为json_file包含网站的来源,而不是实际的内容。 I've tried getURL() and getURLContent(), but without any success. 我尝试过getURL()和getURLContent(),但没有成功。 Any help would be much appreciated! 任何帮助将非常感激!

Edit: as pointed out by Martin Morgan, the problem seems to be with the URL, not the code! 编辑:正如Martin Morgan所指出的,问题似乎是URL,而不是代码!

library(rjson)
fromJSON(readLines('http://toolserver.org/~emw/index.php?c=rawdata&m=get_traffic_data&p1=USA&project1=en&from=12/10/2007&to=4/1/2011')[1])

works for me, with a warning 对我有用,并带有警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM