2013-11-22 36 views
1

撥打電話時這可能是重複的 - 但我找不到解決此問題的內容。來自Google路線API的格式錯誤的JSON從R

我正在使用httpGET()調用google方向API。

套餐:

require(RCurl) 
require(rjson) 
require(gooJSON) 

代碼:

url = "http://maps.googleapis.com/maps/api/directions/json?origin=12.9673293,77.7173975&destination=12.9373613,77.700985&waypoints=optimize:true|12.9723379,77.7117611|12.9922162,77.715895|12.9629354,77.7122996&sensor=false" 
routeJSON = httpGET(url= url) 
routeList = fromJSON(routeJSON) 

我得到:

Error in fromJSON(routeJSON) : 
    unexpected escaped character '\]' at pos 18 

我寫的JSON到一個文件,並將其複製到jsoneditoronline.com。我:

Error: Parse error on line 51: 
...   "points" : "[email protected][\]\ 
-----------------------^ 
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined' 

validated by jsonlint 

但是,當我把網址到瀏覽器並複製輸出到jsoneditoronline它的工作原理。

任何想法爲什麼它正在發生和/或如何規避它?

編輯:我試過gooJSON,但它似乎不支持地圖API V3。

> goomap(url) 
$Status 
$Status$code 
[1] 610 

$Status$request 
[1] "geocode" 

$Status$error_message 
[1] "The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/" 
+0

您應該在您的帖子 – Dason

+0

中添加您正在使用的代碼包。現在就做 – jackStinger

回答

1

下工作正常,我:

require(rjson) 
url = "http://maps.googleapis.com/maps/api/directions/json?origin=12.9673293,77.7173975&destination=12.9373613,77.700985&waypoints=optimize:true|12.9723379,77.7117611|12.9922162,77.715895|12.9629354,77.7122996&sensor=false" 
fromJSON(file=url) 

但是,如果是無效的JSON數據,可調用

fromJSON(url, unexpected.escape="keep") 

來對待轉義字符作爲普通字符(例如\]變爲])