如何在php中解析JSON google maps JSON

如何在php中解析JSON google maps JSON,php,xml,json,parsing,google-maps,Php,Xml,Json,Parsing,Google Maps,如何在php中解析JSON google maps JSON 我已经用xml做了,但是坐标驱动方向并不像我用JSON做的那样完整,我们怎么能用php中的JSON做所有坐标驱动呢 如果我们使用xml $xml = simplexml_load_file('http://maps.googleapis.com/maps/api/directions/xml?origin='.$origin.'&destination='.$dest.'&sensor=false'); $startl

如何在php中解析JSON google maps JSON

我已经用xml做了,但是坐标驱动方向并不像我用JSON做的那样完整,我们怎么能用php中的JSON做所有坐标驱动呢

如果我们使用xml

$xml = simplexml_load_file('http://maps.googleapis.com/maps/api/directions/xml?origin='.$origin.'&destination='.$dest.'&sensor=false');
$startlat = $xml->xpath("/DirectionsResponse/route/leg/step/start_location/lat");
$startlng = $xml->xpath("/DirectionsResponse/route/leg/step/start_location/lng");
$distanceeach = $xml->xpath("/DirectionsResponse/route/leg/step/distance/value");
$distance = $xml->xpath("/DirectionsResponse/route/leg/distance/value");
但是如何处理json呢?
我希望我的变量$STARTAT、$STARTNG等包含相同的值,如果我们使用xml

您可以检索JSON格式的数据,然后使用
JSON_decode()
函数构建
stdClass
对象,然后您可以递归地遍历对象的属性,将其转换为
(数组)$response

然后使用GoogleMapsAPI响应作为数组。您可以不转换为数组,继续使用
stdClass
的对象,如下所示:

$response->DirectionsResponse->route->leg->step->start_location->lat;
要获得正确的值,您可以始终
var\u dump()
选择响应对象,并查看您想要得到什么

希望,这会有帮助


下面是使用谷歌翻译API的谷歌JSON响应的代码。。。这与您需要的基本相同:

if (!is_null($json = json_decode(file_get_contents($url)))
{
    return $json->data->translations[0]->translatedText;
}

就这样…

hai@devdRew你能给我完整的代码吗?我被困在这里:(