从R调用时,来自Google Directions API的格式错误的JSON

时间:2022-04-17 19:42:04

This might be a duplicate- but I couldn't find something to solve this problem.

这可能是重复的 - 但我找不到解决这个问题的方法。

I'm using httpGET() to call the google directions API.

我正在使用httpGET()来调用google指令API。

Packages:

包:

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

the code is:

代码是:

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)

I get:

我明白了:

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

I wrote the JSON to a file and copied it to jsoneditoronline.com. I got:

我将JSON写入文件并将其复制到jsoneditoronline.com。我有:

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

validated by jsonlint

But it works when I put the URL into the browser and copy the output to jsoneditoronline.

但是当我将URL放入浏览器并将输出复制到jsoneditoronline时,它可以工作。

Any idea why it is happening and/or how to circumvent it?

知道它为什么会发生和/或如何绕过它?

EDIT: I tried gooJSON, but it seems it does not support the maps API V3.

编辑:我试过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/"

1 个解决方案

#1


1  

The following works fine for me:

以下工作对我来说很好:

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)

However, if there is invalid json data, one can call

但是,如果有无效的json数据,可以调用

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

to to treat the escaped character as normal character (e.g. \] becomes ])

将转义字符视为普通字符(例如\]成为])

#1


1  

The following works fine for me:

以下工作对我来说很好:

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)

However, if there is invalid json data, one can call

但是,如果有无效的json数据,可以调用

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

to to treat the escaped character as normal character (e.g. \] becomes ])

将转义字符视为普通字符(例如\]成为])