在R中导入JSON文件,并将其作为表进行进一步处理 [英] Import JSON file in R and further process it as table

查看:173
本文介绍了在R中导入JSON文件,并将其作为表进行进一步处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R很陌生.整天都在尝试解决一个问题.不幸的是我无法解决.

I am pretty new with R. Trying to solve one problem already the entire day. Unfortunately I couldn´t solve it.

我想在R中导入JSON文件,然后有机会以与导入CSV文件时相同的方式进行进一步处理.

I´d like to import a JSON file in R and then have the opportunity to further process it in the same way as when I am importing a CSV file.

我的JSON文件必须具有以下结构:

My JSON file has to following structure:

{ "reviewerID": "A2SUAM1J3GNN3B",
  "asin": "0000013714",
  "reviewerName": "J. McDonald",
  "helpful": [2, 3],
  "reviewText": "I bought this for my husband who plays the piano.
  He is having a wonderful time playing these old hymns. The music is at
  times hard to read because we think the book was published for singing
  from more than playing from. Great purchase though!",
  "overall": 5.0,
  "summary": "Heavenly Highway Hymns",
  "unixReviewTime": 1252800000,
  "reviewTime": "09 13, 2009"
}

我想导入JSON文件,然后创建一个包含9列(reviewerID,asin,reviewerName等)的表.

I´d like to import the JSON file and then have a table that consist of 9 columns (reviewerID, asin, reviewerName, etc.).

我使用R包jsonlite进行了尝试,但是如果这样做,则会收到以下错误消息:

I tried it with the R package jsonlite, but if I do so I get the following error message:

 data <- fromJSON('reviews_Office_Products.json.gz2')
 Error in feed_push_parser(buf) : parse error: trailing garbage
      "reviewTime": "07 19, 2013"} {"reviewerID": "A3BBNK2R5TUYGV"
                 (right here) ------^

您知道我可以完成我的工作吗?

Do you have any idea who I can accomplish my undertaking?

非常感谢您.

最诚挚的问候 保罗

推荐答案

最后,我按如下所示进行操作:

finally I did it as follows:

library(rjson)
url <- "reviews_Office_Products.json.gz2"
con = file(url, "r")
input <- readLines(con, -1L)
my_results <- lapply(X=input,fromJSON)

close(con)
tr.review <- ldply(lapply(input, function(x) t(unlist(fromJSON(x)))))
save(tr.review, file= 'tr.review.rdata')

出于我的目的,这可行,并且我可以使用tm-package进一步处理数据.

For my purposes this works and I can further process the data with the tm-package.

非常感谢您的帮助. 保罗

Thank you very much for your help. Paul

这篇关于在R中导入JSON文件,并将其作为表进行进一步处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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