R jsonlite - fromJSON always returns Error in open.connection?

做~自己de王妃 提交于 2020-01-07 03:11:29

问题


Why do I always get the connection error with fromJSON? Sometimes it is fine. Most of time it is not.

> # Load json and other packages.
> library(jsonlite)
> 
> # Live server.
> server <- 'http://0.0.0.0:3000'
> # Stream 143
> key <- '9p06nngO2pcQM03nIJ71dLXNA1v'
> 
> # Retrieve json data from the data platform via the URLs.
> streams <- fromJSON(paste(server, '/output/streams', sep=""), flatten=TRUE)

Result:

Error in open.connection(con, "rb") : Couldn't connect to server

If I try again:

> streams <- fromJSON(paste(server, '/output/streams', sep=""), flatten=TRUE)

Error:

Error in open.connection(con, "rb") : Server returned nothing (no headers, no data)

But the server is fine. It is up and running.

Any ideas how I can fix this?

Or any other better and more reliable package for getting the json data from the server?


回答1:


It seems to me that there is some special character in the public_key field. For instance:

Gurl<-"http://139.162.208.52:3000/output/stream?public_key=9p06nngO2pcQM03nIJ71dLXNA1v"
Burl<-"http://139.162.208.52:3000/output/stream?public_key=9p06nngO‌​2pcQM03nIJ71dLXNA1v"
#this seems to work
fromJSON(Gurl, flatten=TRUE)
#this doesn't
fromJSON(Burl, flatten=TRUE)

Burl and Gurl are only apparently the same:

Gurl==Burl
#[1] FALSE

You can inspect the raw content of the above urls (with charToRaw) and check the Encoding to spot the differences.




回答2:


Eg. Php, basic just get data from server by json_encode What programming languages are u using? For back-end



来源:https://stackoverflow.com/questions/42739594/r-jsonlite-fromjson-always-returns-error-in-open-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!