在R中读取JSON文件时出现问题 [英] Problems reading JSON file in R

查看:201
本文介绍了在R中读取JSON文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件(从mongoDB导出),我想将其加载到R中.该文档的大小约为890 MB,包含12个字段的大约63,000行.这些字段是数字,字符和日期.我想得到一个63000 x 12的数据帧.

I have a JSON file (an export from mongoDB) that I'd like to load into R. The document is about 890 MB in size with roughly 63,000 rows of 12 fields. The fields are numeric, character and date. I'd like to end up with a 63000 x 12 data frame.

lines <-  readLines("fb2013.json")

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

result: jFile has all 63,000 elements in char class and all fields are lumped into one field.

每个文件如下所示:

"{\" _ 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 \:\ "photo \",\"updated_at \":{\"$ date \":1358210153000}}"

"{ \"_id\" : \"10151271769737669\", \"comments_count\" : 36, \"created_at\" : { \"$date\" : 1357941938000 }, \"icon\" : \"http://blahblah.gif\", \"likes_count\" : 450, \"link\" : \"http://www.blahblahblah.php\", \"message\" : \"I wish I could figure this out!\", \"page_category\" : \"Computers\", \"page_id\" : \"30968999999\", \"page_name\" : \"NothingButTrouble\", \"type\" : \"photo\", \"updated_at\" : { \"$date\" : 1358210153000 } }"

使用rjson,

jFile <- fromJSON(paste(readLines("fb2013.json"), collapse=""))

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

only the first row is read into jFile but there are 12 fields.

使用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个字段.

Again, only the first row is read into jFile and there are 12 fields.

rjson和RJSONIO的输出如下所示:

The output from rjson and RJSONIO looks something like this:

$`_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 

推荐答案

尝试

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

这篇关于在R中读取JSON文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆