R - Error in fromJSON(raw.data) : incomplete list

故事扮演 提交于 2019-12-08 12:57:52

问题


I'm trying to read API data from the BLS into R. I am using the Version 1.0 that does not require registration and is open for public use.

Here is my code:

url <-"http://api.bls.gov/publicAPI/v1/timeseries/data/LAUCN040010000000005"
raw.data <- readLines(url, warn = F)

library(rjson)

rd <- fromJSON(raw.data)

And here is the error message I receive:

Error in fromJSON(raw.data) : incomplete list

If I just try to go to the url in my webrowser it seems to work (pull up a JSON webpage). Not really sure what is going on when I try to get this into R.


回答1:


When you've used readLines, the object returned is a vector of length 4:

length(raw.data)

You can look at the individual pieces via:

raw.data[1]

If you stick the pieces back together using paste

fromJSON(paste(raw.data, collapse = ""))

everything works. Alternatively,

jsonlite::fromJSON(url)


来源:https://stackoverflow.com/questions/36799124/r-error-in-fromjsonraw-data-incomplete-list

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