2017-08-20 1 views
0

最近GoogleからJSON応答を解析しようとするとこのエラーが発生します。字句エラー:文字列の中で文字の前に ''が表示される/ R内でjsonliteを使用する

lexical error: inside a string, '\' occurs before a character which it may not. 
     [[email protected]@KBKBGDGDOJ]\[email protected]@E?IAGAGAICGEGEIGIMOS 
       (right here) ------^ 

Googleがお送りとしてブラウザで表示した文字列は、この(私はそれを短縮)のようになります。

[[email protected]@KBKBGDGDOJ]\\[email protected]@E? 

これは

route_doc <- getURL(route) 
route_response <- fromJSON(route_doc) 

どういうわけかjsonliteハングを使用してコード - 私でありますここに。 Googleからの応答を解析するにはどうすればよいですか?それを少し調査した後

:ポリラインエントリはバックスラッシュ文字

"polyline" : { 
        "points" : "qv{[email protected]?" 
       }, 

ありがとう含まれている場合 このクラッシュが起こります!

アップデート: - 私はGoogleからのJSON応答を取得するには、このコードを使用して:

route_from="Köln" 
route_to="Hamburg" 

get_route_url<-function(origin, 
destination, 
avoid,mode, 
alternatives, 
key, 
return.call = "json") 

{ 
    root<-"https://maps.googleapis.com/maps/api/directions/" 
    u<-paste(root, 
      return.call, 
      "?origin=", origin, 
      "&destination=",destination, 
      "&avoid=",avoid, 
      "&mode=", mode, 
      "&alternatives=",alternatives, 
      "&key=",key, 
      sep = "") 
    return(URLencode(u)) 
    } 


route<-get_route_url(route_from, 
        route_to, 
        "highways", 
        "driving", 
        "true",MYKEY) 

route_doc <- getURL(route) 

route_response <- fromJSON(route_doc) 

とポリラインの例のエラー:

lexical error: inside a string, '\' occurs before a character which it may not. 
      [email protected]@[email protected]`@@[email protected]@\G"      },  
        (right here) ------^ 
+0

使用 'ライブラリ(googleway)を参照するには上。 google_directions(...) 'または' library(ggmap); route(...) ' – SymbolixAU

+0

他の解決法がうまくいかない場合は、私は行います。私は自分自身のGoogle APIへの呼び出しを構築したいと思います。だから、このクラッシュは常に "ポリライン"項目に起こります。 – Andreas

+0

Googleからの回答を得るために使用しているコードを投稿できますか? – SymbolixAU

答えて

1

GoogleのAPIは、JSONでレスポンスを返しますfromJSONはJSONを直接読み取ることができるので、を使用して 'doc'を実際に取得する必要はありません。

あなたのrouteオブジェクト

route_response <- fromJSON(route) 

とルート

library(googleway) 
mapKey <- 'my_map_key' 

df <- data.frame(polyline = route_response$routes$overview_polyline$points) 

google_map(key = mapKey) %>% 
    add_polylines(data = df, polyline = "polyline") 

enter image description here

関連する問題