代码是
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"}
有什么问题?谢谢!
答案 0 :(得分:1)
您的文件不包含有效的JSON。你基本上有三个JSON哈希坐在一起。确定分隔值的空格的确切选择并不重要。它等同于:
{} {} {}
这就像三个基元紧挨着坐在一起一样无效:
3 'a' true
一般来说,当函数输入无效时,所有投注都会关闭。希望编写函数以优雅地失败并发出描述无效性质的明确错误消息,并且经常是这种情况,但这并不总是发生。在这种情况下,遇到这种无效JSON时rjson::fromJSON()
似乎正在做的是解析并返回第一个值,并默默地忽略其他所有内容。这很不幸,但我们能做些什么。
您应该调查文件的生成方式,并在此结束时寻求纠正问题。但是如果你想破解解决方案,我们可以将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"
##
##