6

我有以下 JSON 文件:

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

我使用了 jsonlite 包,但出现解析错误

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) ------^
4

2 回答 2

4

尝试将您的数据文件更改为以下

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

相同的代码对我有用..它正在寻找一个数组..

于 2016-02-08T18:33:49.397 回答
2

您的文件是换行符分隔的 JSON ( http://ndjson.org/ )。您可以像这样使用 jsonlite 阅读它:

try <- stream_in(file("states.txt"))
于 2018-03-23T15:37:26.117 回答