從來沒有這樣做過,並且對格式和如何訪問特定變量感到困惑。php解析json數據
結果:
$result = json_decode($result);
print_r($result);
是:
stdClass Object
(
[input] => stdClass Object
(
[address_components] => stdClass Object
(
[number] => 1109
[predirectional] => N
[street] => Highland
[suffix] => St
[formatted_street] => N Highland St
[city] => Arlington
[state] => VA
[zip] => 22201
[country] => US
)
[formatted_address] => 1109 N Highland St, Arlington, VA 22201
)
[results] => Array
(
[0] => stdClass Object
(
[address_components] => stdClass Object
(
[number] => 1109
[predirectional] => N
[street] => Highland
[suffix] => St
[formatted_street] => N Highland St
[city] => Arlington
[county] => Arlington County
[state] => VA
[zip] => 22201
[country] => US
)
[formatted_address] => 1109 N Highland St, Arlington, VA 22201
[location] => stdClass Object
(
[lat] => 38.886672
[lng] => -77.094735
)
[accuracy] => 1
[accuracy_type] => rooftop
[source] => Arlington
)
[1] => stdClass Object
(
[address_components] => stdClass Object
(
[number] => 1109
[predirectional] => N
[street] => Highland
[suffix] => St
[formatted_street] => N Highland St
[city] => Arlington
[county] => Arlington County
[state] => VA
[zip] => 22201
[country] => US
)
[formatted_address] => 1109 N Highland St, Arlington, VA 22201
[location] => stdClass Object
(
[lat] => 38.886665
[lng] => -77.094733
)
[accuracy] => 1
[accuracy_type] => rooftop
[source] => Virginia Geographic Information Network (VGIN)
)
)
)
我如何可以訪問緯度和經度值?由於混合了對象和數組,因此我感到困惑。我從來沒有用這種方式與PHP的JSON合作...這是一個地理編碼api,返回JSON格式。
我基本上想要做一些類似$lat = $result['results'][0]['location']['lat'];
等等。
使用類型轉換,將其轉換成對象 –