我很困惑如何在PHP中解析JSON对象。
我有这个对象从谷歌API(我已经删除了我的地址详细信息!):
stdClass Object (
[routes] => Array (
[0] => stdClass Object (
[bounds] => stdClass Object (
[northeast] => stdClass Object (
[lat] => 0.000000
[lng] => 0.000000
)
[southwest] => stdClass Object (
[lat] => 0.000000
[lng] => 0.00000
)
)
[copyrights] => Map data ©2013 Google
[legs] => Array (
[0] => stdClass Object (
[distance] => stdClass Object (
[text] => 1 ft
[value] => 0
)
[duration] => stdClass Object (
[text] => 1 min
[value] => 0
)
[end_address] => xxxx
[end_location] => stdClass Object (
[lat] => 0.0000
[lng] => 0.0000
)
[start_address] => xx
[start_location] => stdClass Object (
[lat] => 0.0000
[lng] => 0.0000
)
[steps] => Array (
[0] => stdClass Object (
[distance] => stdClass Object (
[text] => 1 ft
[value] => 0
)
[duration] => stdClass Object (
[text] => 1 min
[value] => 0
)
[end_location] => stdClass Object (
[lat] => 0.0000
[lng] => 0.0000
)
[html_instructions] => xxx
[polyline] => stdClass Object (
[points] => wcrnIpdvH
)
[start_location] => stdClass Object (
[lat] => 1.234567
[lng] => 1.234567
)
[travel_mode] => DRIVING
)
)
[via_waypoint] => Array (
)
)
)
[overview_polyline] => stdClass Object (
[points] => wcrnIpdvH
)
[summary] => xx
[warnings] => Array (
)
[waypoint_order] => Array (
)
)
)
[status] => OK
)
我想得到我作为[lat] => 1.234567 [lng] => 1.234567
留下的最后一个纬度和经度
我已经想出了如何通过这样做来获得距离:
$directions = json_decode($return);
$distance = $directions->routes[0]->legs[0]->distance->text;
然而,获取经度和纬度让我很困惑
如果你能解释一下JSON,谢谢!
尝试以下操作:
//northeast
$lat = $directions->routes[0]->bounds->northeast->lat;
$lng = $directions->routes[0]->bounds->northeast->lng;
和
////southwest
$lat = $directions->routes[0]->bounds->southwest->lat;
$lng = $directions->routes[0]->bounds->southwest->lng;