我很困惑如何在 PHP 中解析 JSON 对象。
我从 Google 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,那就更感谢了!