从R中的JSON文件获取数据

从R中的JSON文件获取数据,json,r,Json,R,假设我有以下json文件: { "id": "000018ac-04ef-4270-81e6-9e3cb8274d31", "currentCompany": "", "currentTitle": "--", "currentPosition": "" } 我使用以下代码: Usersfile <- ('trial.json') #where trial the json above library('rjson') c <- file(Usersfile,

假设我有以下json文件:

{
  "id": "000018ac-04ef-4270-81e6-9e3cb8274d31",
   "currentCompany": "",
   "currentTitle": "--",
   "currentPosition": ""
}
我使用以下代码:

Usersfile <- ('trial.json') #where trial the json above
library('rjson')
c <- file(Usersfile,'r')
l <- readLines(c,-71L)
json <- lapply(X=l,fromJSON)
但当我输入json文件(带记事本)并将数据放在一行时:

{"id": "000018ac-04ef-4270-81e6-9e3cb8274d31","currentCompany": "","currentTitle": "--","currentPosition": ""}
代码运行得很好。(实际上,文件非常大,需要手动为每行执行此操作)。为什么会这样?我怎样才能克服这一点

这一个也不起作用:

{ "id": "000018ac-04ef-4270-81e6-9e3cb8274d31","currentCompany": "","currentTitle": "--","currentPosition": ""
}
编辑:我使用了以下代码,我只能读取第一个值:

library('rjson')
c <- file.path(Usersfile)
data <- fromJSON(file=c)
library('rjson')

c很惊讶,这个问题一直没有得到回答!使用jsonlite包,您可以使用
粘贴(x,collapse=“”)
删除EOF标记以正确导入到R数据帧中,将json数据折叠为一个字符元素。一、 同样,面对一个打印精美的json,有着确切的错误:

library(jsonlite)

json <- do.call(rbind, 
                lapply(paste(readLines(Usersfile, warn=FALSE),
                             collapse=""), 
                       jsonlite::fromJSON))
library(jsonlite)
json
library(jsonlite)

json <- do.call(rbind, 
                lapply(paste(readLines(Usersfile, warn=FALSE),
                             collapse=""), 
                       jsonlite::fromJSON))