2016-05-10 2 views
0

で読み取った、しかし、返される結果は、9つだけのJSONラインがfile.jsonで1000の以上の行があるコードが</p> <pre><code>library(rjson) url <- 'file.json' j <- fromJSON(file=url, method='C') </code></pre> <p>あるrjson

ファイルのリストです。 .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

@bgoldstサンプルを編集しました。私はコードを使ってファイルの最初の行だけを取得できます。 – user3329081

答えて

1

ファイルに有効なJSONが含まれていません。あなたは基本的に3つのJSONハッシュをお互いのすぐ隣に置いています。値を区切る空白の正確な選択は重要ではありません。これは、これに同等です:関数への入力は、すべてのベット無効であるとき

3 'a' true 

は、一般的に言えば:それは3つのプリミティブがお互いに右隣に座っていたかのように同じように無効だ

{} {} {} 

オフです。正常に機能しないように関数を記述し、無効の性質を記述する明確なエラーメッセージを出力することが望ましく、非常に頻繁に起こりますが、必ずしもそうであるとは限りません。この場合、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" 
## 
## 
関連する問題