2010-08-06 112 views
4

我嘗試從R使用rjson讀取JSON文件,但仍然出現錯誤。我使用各種在線驗證器驗證了JSON文件。這裏是JSON文件的內容:從R讀取JSON文件

{ 
    "scenarios": [ 
     { 
     "files": { 
      "type1": "/home/blah/Desktop/temp/scen_0.type1", 
      "type2": "/home/blah/Desktop/temp/scen_0.type2" 
     }, 
     "ID": "scen_0", 
     "arr": [], 
     "TypeToElementStatsFilename": { 
      "type1": "/home/blah/Desktop/temp/scen_0.type1.elements", 
      "type2": "/home/blah/Desktop/temp/scen_0.type2.elements" 
     } 
     } 
    ], 
    "randomSeed": "39327314969888", 
    "zone": { 
     "length": 1000000, 
     "start": 1 
    }, 
    "instanceFilename": "/home/blah/bloo/data/XY112.zip", 
    "txtFilename": "/home/blah/bloo/data/XY112.txt", 
    "nSimulations": 2, 
    "TypeTodbFilename": { 
     "type1": "/home/blah/bloo/data/map.type1.oneAmb.XY112.out" 
    }, 
    "arr": { 
     "seg11": { 
     "length": 1000, 
     "start": 147000 
     }, 
     "seg12": { 
     "length": 1000, 
     "start": 153000 
     }, 
     "seg5": { 
     "length": 1000, 
     "start": 145000 
     }, 
     "seg6": { 
     "length": 1000, 
     "start": 146000 
     }, 
     "seg1": { 
     "length": 100, 
     "start": 20000 
     } 
    }, 
    "outPath": "/home/blah/Desktop/temp", 
    "instanceID": "XY112", 
    "arrIds": [ 
     "seg5", 
     "seg6", 
     "seg1", 
     "seg11", 
     "seg12" 
    ], 
    "truth": { 
     "files": { 
     "type1": "/home/blah/Desktop/temp/truth.type1", 
     "type2": "/home/blah/Desktop/temp/truth.type2" 
     }, 
     "ID": "truth", 
     "TypeToElementStatsFilename": { 
     "type1": "/home/blah/Desktop/temp/truth.type1.elements", 
     "type2": "/home/blah/Desktop/temp/truth.type2.elements" 
     } 
    } 
} 

和錯誤:

> json_file <- "~/json" 
> json_data <- fromJSON(paste(readLines(json_file), collapse="")) 
Error in fromJSON(paste(readLines(json_file), collapse = "")) : 
    unexpected character: : 
+0

如果你告訴我們,這將有助於什麼版本* rjson的*。這到2013年仍然是一個懸而未決的問題嗎? – smci 2013-04-17 07:38:21

回答

6

RJSON怪胎約空數組。

fromJSON('{ "arr": [ ] }')

錯誤fromJSON( 「{\」 ARR \ 「:[]}」):意外字符:

+0

+1謝謝。顯然它不僅在空數組上失敗,而且在空對象上失敗。我會發郵件給維護者,因爲我認爲這些在JSON中是有效的。 – 2010-08-06 08:35:42

2

您可以嘗試在RJSONIO包在​​主辦的fromJSON功能。它似乎讀取的文件很好。

0

這是一個修復。

創建一個新函數來替換RCurl中使用的現有getURL函數,並且您應該有解決方案。

myGetURL <- function(...) { 
    rcurlEnv <- getNamespace("RCurl") 
    mapUnicodeEscapes <- get("mapUnicodeEscapes", rcurlEnv) 
    unlockBinding("mapUnicodeEscapes", rcurlEnv) 
    assign("mapUnicodeEscapes", function(str) str, rcurlEnv) 
    on.exit({ 
     assign("mapUnicodeEscapes", mapUnicodeEscapes, rcurlEnv) 
     lockBinding("mapUnicodeEscapes", rcurlEnv) 
    }, add = TRUE) 
    return(getURL(...)) 
} 

測試:

> json <- myGetURL("http://abicky.net/hatena/rcurl/a.json") 
> cat(json, fill = TRUE) 
{"a":"\\\"\u0030\\\""} 
> fromJSON(json) 
$a 
[1] "\\\"0\\\""