0

代码是

library(rjson)
url <- 'file.json'
j <- fromJSON(file=url, method='C')

file.json 有 1000 多行,但是返回的结果是 9 个列表。

file.json 是

{"reviewerID": "A30TL5EWN6DFXT", "asin": "120401325X", "reviewerName": "christina", "helpful": [0, 0], "reviewText": "They look good and stick good! I just don't like the rounded shape because I was always bumping it and Siri kept popping up and it was irritating. I just won't buy a product like this again", "overall": 4.0, "summary": "Looks Good", "unixReviewTime": 1400630400, "reviewTime": "05 21, 2014"}
{"reviewerID": "ASY55RVNIL0UD", "asin": "120401325X", "reviewerName": "emily l.", "helpful": [0, 0], "reviewText": "These stickers work like the review says they do. They stick on great and they stay on the phone. They are super stylish and I can share them with my sister. :)", "overall": 5.0, "summary": "Really great product.", "unixReviewTime": 1389657600, "reviewTime": "01 14, 2014"}
{"reviewerID": "A2TMXE2AFO7ONB", "asin": "120401325X", "reviewerName": "Erica", "helpful": [0, 0], "reviewText": "These are awesome and make my phone look so stylish! I have only used one so far and have had it on for almost a year! CAN YOU BELIEVE THAT! ONE YEAR!! Great quality!", "overall": 5.0, "summary": "LOVE LOVE LOVE", "unixReviewTime": 1403740800, "reviewTime": "06 26, 2014"}

问题是什么?谢谢!

4

1 回答 1

1

您的文件不包含有效的 JSON。您基本上拥有三个相邻的 JSON 哈希。分隔值的空格的确切选择无关紧要。这相当于:

{} {} {}

这就像三个原语并排坐在一起一样无效:

3 'a' true

一般来说,当函数的输入无效时,所有的赌注都会被取消。编写函数以优雅地失败并发出描述无效性质的清晰错误消息是可取的,这种情况经常发生,但并不总是发生。在这种情况下,rjson::fromJSON()当它遇到这种无效的 JSON 时,似乎在做的就是解析并返回第一个值,而默默地忽略其他所有内容。这很不幸,但我们能做些什么。

您可能应该调查该文件是如何生成的,并在此结束时寻求纠正问题。但是如果你想破解一个解决方案,我们可以将 JSON 的行读入一个字符向量,将它们粘贴到逗号上,在结果字符串周围粘贴括号分隔符,然后解析该字符串以获得一个哈希数组。这只有在每个相邻的散列恰好占据文件中的一行时才有效。

fromJSON(paste0('[',paste(collapse=',',readLines(url)),']'));
## [[1]]
## [[1]]$reviewerID
## [1] "A30TL5EWN6DFXT"
##
## [[1]]$asin
## [1] "120401325X"
##
## [[1]]$reviewerName
## [1] "christina"
##
## [[1]]$helpful
## [1] 0 0
##
## [[1]]$reviewText
## [1] "They look good and stick good! I just don't like the rounded shape because I was always bumping it and Siri kept popping up and it was irritating. I just won't buy a product like this again"
##
## [[1]]$overall
## [1] 4
##
## [[1]]$summary
## [1] "Looks Good"
##
## [[1]]$unixReviewTime
## [1] 1400630400
##
## [[1]]$reviewTime
## [1] "05 21, 2014"
##
##
## [[2]]
## [[2]]$reviewerID
## [1] "ASY55RVNIL0UD"
##
## [[2]]$asin
## [1] "120401325X"
##
## [[2]]$reviewerName
## [1] "emily l."
##
## [[2]]$helpful
## [1] 0 0
##
## [[2]]$reviewText
## [1] "These stickers work like the review says they do. They stick on great and they stay on the phone. They are super stylish and I can share them with my sister. :)"
##
## [[2]]$overall
## [1] 5
##
## [[2]]$summary
## [1] "Really great product."
##
## [[2]]$unixReviewTime
## [1] 1389657600
##
## [[2]]$reviewTime
## [1] "01 14, 2014"
##
##
## [[3]]
## [[3]]$reviewerID
## [1] "A2TMXE2AFO7ONB"
##
## [[3]]$asin
## [1] "120401325X"
##
## [[3]]$reviewerName
## [1] "Erica"
##
## [[3]]$helpful
## [1] 0 0
##
## [[3]]$reviewText
## [1] "These are awesome and make my phone look so stylish! I have only used one so far and have had it on for almost a year! CAN YOU BELIEVE THAT! ONE YEAR!! Great quality!"
##
## [[3]]$overall
## [1] 5
##
## [[3]]$summary
## [1] "LOVE LOVE LOVE"
##
## [[3]]$unixReviewTime
## [1] 1403740800
##
## [[3]]$reviewTime
## [1] "06 26, 2014"
##
##
于 2016-05-10T03:01:23.107 回答