在R中读取JSON文件时出现问题

SCa*_*lan 3 json r rjsonio rjson

我有一个JSON文件(从mongoDB导出),我想加载到R.文件大小约为890 MB,大约63,000行12个字段.字段是数字,字符和日期.我想最终得到一个63000 x 12数据帧.

lines <-  readLines("fb2013.json")
Run Code Online (Sandbox Code Playgroud)

结果:jFile在char类中包含所有63,000个元素,并且所有字段都集中在一个字段中.

每个文件看起来像这样:

"{\"_ id \":\"10151271769737669 \",\"comments_count \":36,\"created_at \":{\"$ date \":1357941938000},\"icon \":\"http:/ /blahblah.gif \",\"likes_count \":450,\"link \":\"http://www.blahblahblah.php \",\"message \":\"我希望我能搞清楚这一点!\",\"page_category \":\"Computers \",\"page_id \":\"30968999999 \",\"page_name \":\"NothingButTrouble \",\"type \":\"photo\",""updated_at \":{\"$ date \":1358210153000}}"

使用rjson,

jFile <- fromJSON(paste(readLines("fb2013.json"), collapse=""))
Run Code Online (Sandbox Code Playgroud)

只有第一行被读入jFile,但有12个字段.

使用RJSONIO:

jFile <- fromJSON(lines)
Run Code Online (Sandbox Code Playgroud)

结果如下:

Warning messages:
1: In if (is.na(encoding)) return(0L) :
  the condition has length > 1 and only the first element will be used
Run Code Online (Sandbox Code Playgroud)

同样,只有第一行被读入jFile并且有12个字段.

rjson和RJSONIO的输出看起来像这样:

$`_id`
[1] "1018535"

$comments_count
[1] 0

$created_at
       $date 
1.357027e+12 

$icon
[1] "http://blah.gif"

$likes_count
[1] 20

$link
[1] "http://www.chachacha"

$message
[1] "I'd love to figure this out."

$page_category
[1] "Internet/software"

$page_id
[1] "3924395872345878534"

$page_name
[1] "Not Entirely Hopeless"

$type
[1] "photo"

$updated_at
       $date 
1.357027e+12 
Run Code Online (Sandbox Code Playgroud)

jin*_*ong 9

尝试

library(rjson)
path <- "WHERE/YOUR/JSON/IS/SAVED"
c <- file(path, "r")
l <- readLines(c, -1L)
json <- lapply(X=l, fromJSON)
Run Code Online (Sandbox Code Playgroud)