JSON (using jsonlite) parsing error in R

懵懂的女人 提交于 2019-12-07 11:47:36

问题


I have the following JSON file:

{"id":1140854908,"name":"'Amran"} 
{"id":1140852651,"name":"'Asir"} 
{"id":1140855190,"name":"'Eua"} 
{"id":1140851307,"name":"A Coruna"} 
{"id":1140854170,"name":"A`Ana"}

I used the package jsonlite but I get a parsing error

library(jsonlite) 
try <- fromJSON("states.txt",simplifyDataFrame = T)
# Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :   
# parse error: trailing garbage
#           :1140854908,"name":"'Amran"} {"id":1140852651,"name":"'Asir"
#                      (right here) ------^

回答1:


Try changing your data file to below

[
{"id":1140854908,"name":"'Amran"} 
,{"id":1140852651,"name":"'Asir"} 
,{"id":1140855190,"name":"'Eua"} 
,{"id":1140851307,"name":"A Coruna"} 
,{"id":1140854170,"name":"A`Ana"}
]

The same code worked for me.. It is looking for an array..




回答2:


Your file is a newline delimited JSON (http://ndjson.org/). You can read it with jsonlite like this:

try <- stream_in(file("states.txt"))


来源:https://stackoverflow.com/questions/35276184/json-using-jsonlite-parsing-error-in-r

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