2016-04-02 12 views
1

JSON biçimindeki bir sunucudan aldığım verileri aldım. Şimdi R. bu verilerin (bir metin editörü açılmış ise)Biçim JSON dosyası R: karakter kodlamasına sahip sözcük hatası

Benim ham .json dosyası aşağıdaki gibi görünür-sürecini önceden istiyorum:

{"id": 1,"data": "{\"unid\":\"wU6993\",\"age\":\"21\",\"origin\":\"Netherlands\",\"biling\":\"2\",\"langs\":\"Dutch\",\"selfrating\":\"80\",\"selfarrest\":\"20\",\"condition\":1,\"fly\":\"2\",\"flytime\":0,\"purpose\":\"na\",\"destin\":\"Madrid\",\"txtQ1\":\"I\'m flying to Madrid to catch up with friends.\"}"}

ben daha için geri ayrıştırmak istiyor Ben hiç bunu okuyamaz

`{ 

"id": 1, 

"data": { 

    "unid": "wU6993", 

    "age": "21", 

    "origin": "Netherlands", 

    "biling": "2", 

    "langs": "Dutch", 

    "selfrating": "80", 

    "selfarrest": "20", 

    "condition": 1, 

    "fly": "2", 

    "flytime": 0, 

    "purpose": "na", 

    "destin": "Madrid", 

    "txtQ1": "I'm flying to Madrid to catch up with friends." 

} 

}` 

jsonlite kullanılması:

parsed = jsonlite::fromJSON(txt = 'exp1.json') 
amaçlanan biçimine kullanmak
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
    lexical error: inside a string, '\' occurs before a character which it may not. 
      in\":\"Madrid\",\"txtQ1\":\"I\'m flying to Madrid to catch u 
        (right here) ------^ 

hata bazı karakterler o olmalıydı kaçan yaşandığını anlatıyor düşünüyorum.

Bunu nasıl çözebilirim ve dosyamı okuyabilir miyim?

cevap

2

"data"'u tanımlayan iç içe geçmiş parantezlerin çevresinde ek bir teklifiniz var; bu değer, geçerli JSON yerine gerçekte bir büyük dize olarak saklanıyor. Onları dışarı çıkarın ve

my_json <- '{"id": 1,"data": "{\"unid\":\"wU6993\",\"age\":\"21\",\"origin\":\"Netherlands\",\"biling\":\"2\",\"langs\":\"Dutch\",\"selfrating\":\"80\",\"selfarrest\":\"20\",\"condition\":1,\"fly\":\"2\",\"flytime\":0,\"purpose\":\"na\",\"destin\":\"Madrid\",\"txtQ1\":\"I\'m flying to Madrid to catch up with friends.\"}"}' 

my_json <- sub('"\\{', '\\{', my_json) 
my_json <- sub('\\}"', '\\}', my_json) 

jsonlite::prettify(my_json) 
# { 
#  "id": 1, 
#  "data": { 
#   "unid": "wU6993", 
#   "age": "21", 
#   "origin": "Netherlands", 
#   "biling": "2", 
#   "langs": "Dutch", 
#   "selfrating": "80", 
#   "selfarrest": "20", 
#   "condition": 1, 
#   "fly": "2", 
#   "flytime": 0, 
#   "purpose": "na", 
#   "destin": "Madrid", 
#   "txtQ1": "I'm flying to Madrid to catch up with friends." 
#  } 
# }