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 が含まれていません。基本的に、3 つの JSON ハッシュが隣り合って配置されています。値を区切る空白の正確な選択は重要ではありません。これは次と同等です:

{} {} {}

これは、3 つのプリミティブが隣り合っているのと同じように無効です。

3 'a' true

一般的に言えば、関数への入力が無効な場合、すべての賭けは無効になります。適切に失敗し、無効性の性質を説明する明確なエラー メッセージを出力するように関数を記述することが望ましいです。非常に多くの場合、これが当てはまりますが、常にそうとは限りません。この場合、rjson::fromJSON()この種の無効な JSON に遭遇したときに行っているように見えるのは、最初の値を解析して返し、他のすべてを静かに無視することです。それは残念ですが、私たちに何ができるでしょうか。

おそらく、ファイルがどのように生成されたかを調査し、最後に問題を修正する必要があります。しかし、解決策をハックしたい場合は、JSON の行を文字ベクトルに読み込み、カンマでそれらを貼り付けて折りたたんで、結果の文字列の周りにブラケット区切り記号を貼り付け、その文字列を解析してハッシュの配列を取得できます。これは、隣接する各ハッシュがファイル内で正確に 1 行を占める場合にのみ機能します。

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 に答える