R jsonlite - fromJSON always returns Error in open

2019-09-15 09:22发布

问题:

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



标签: r shiny jsonlite