我有一个 JSON 文件(从 mongoDB 导出),我想将它加载到 R 中。该文档大小约为 890 MB,包含 12 个字段的大约 63,000 行。这些字段是数字、字符和日期。我想最终得到一个 63000 x 12 的数据框。
lines <- readLines("fb2013.json")
结果: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\" : \"计算机\", \"page_id\" : \"30968999999\", \"page_name\" : \"NothingButTrouble\", \"type\" : \"照片\ ", \"updated_at\" : { \"$date\" : 1358210153000 } }"
使用 rjson,
jFile <- fromJSON(paste(readLines("fb2013.json"), collapse=""))
只有第一行被读入 jFile 但有 12 个字段。
使用 RJSONIO:
jFile <- fromJSON(lines)
结果如下:
Warning messages:
1: In if (is.na(encoding)) return(0L) :
the condition has length > 1 and only the first element will be used
同样,只有第一行被读入 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