multiple JSON objects in fromJSON

左心房为你撑大大i 提交于 2019-11-30 09:52:19

问题


I'm trying to use fromJSON() to read in a .json file with multiple objects structured as follows:

{ "key11": value11, "key12": value12 }
{ "key11": value11, "key12": value12 }
…

If I manually add [...] brackets around the entire file, and , commas between the objects, then the following code works:

json_file <- "file.json"
json_data <- fromJSON(json_file,flatten=TRUE)

But adding the brackets and commas is not feasible for what I'm actually trying to do (I did it on a sample file).

I tried using readLines() since the objects have newlines between them, but I didn't get anywhere.

How can I read in these json objects without modifying the file?


回答1:


Assuming you know it's one per line, and no weird line breaks,

 lapply(readLines(filename), fromJSON, flatten = TRUE)



回答2:


if you read references: the option unexpected.escape is available.

unexpected.escape changed handling of unexpected escaped characters. Handling value should be one of "error", "skip", or "keep"; on unexpected characters issue an error, skip the character, or keep the character



来源:https://stackoverflow.com/questions/27023972/multiple-json-objects-in-fromjson

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