Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

r - doing multiple fromJSON queries (jsonlite)

I'm a newbie R user and couldn't find any answers which I could understand.

My objective is to retrieve information from a URL and then convert the information into a data frame for use.

install.packages("jsonlite")

library(jsonlite)

fromJSON("https://developers.onemap.sg/commonapi/search?searchVal=revenue&returnGeom=Y&getAddrDetails=Y&pageNum=1)") 
#it works

x1 <- as.character("https://developers.onemap.sg/commonapi/search?searchVal=revenue&returnGeom=Y&getAddrDetails=Y&pageNum=1)")

fromJSON(x1) #also works

fromJSON(paste("x",1,sep=""))

=> Error: lexical error: invalid char in json text.    

Where did I go wrong? I verified that paste("x",1,sep=="") is equivalent to "x1", so shouldn't it work?

In any case, I was actually planning to put a list of postal codes in a data frame to replace each searchVal= to generate a list of URLs, get a data frame for each URL then merge them all together. But given the difficult I face above, I guess I will try to get help from the above first.

Thanks a lot in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you are trying to use a string as a variable, in current set up R knows x1 however 'x1' is nothing but just a string. I hope someone might be able to explain better. So you need to use. eval.

fromJSON(eval(parse(text=paste('x',1,sep=''))))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...