2010-08-06 6 views
4

rjson을 사용하여 R에서 JSON 파일을 읽으려고 시도하지만 오류가 계속 발생합니다. 다양한 온라인 유효성 검사기를 사용하여 JSON 파일의 유효성을 검사했습니다.JSON 파일 읽기 R

{ 
    "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 파일의 내용은

> 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

답변

6

RJSON는 빈 배열에 대해 알아 괴물. fromJSON에서

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

오류 ("{\"편곡 \ ": []}") : 예기치 않은 문자 :

+0

+1 감사합니다. 분명히 그것은 빈 배열뿐만 아니라 빈 객체에서도 실패합니다. JSON에서 유효하다고 생각하기 때문에 유지 자에게 이메일을 보내 드리겠습니다. –

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\\\""