2017-10-10 85 views
3

從來沒有這樣做過,並且對格式和如何訪問特定變量感到困惑。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'];等等。

+1

使用類型轉換,將其轉換成對象 –

回答

3

你必須json_decode使用true作爲第二個參數,以便輸出將是正常的PHP數組不stdclass陣列(這樣就可以用你的方法,你正在嘗試什麼)

所以做象下面這樣: -

$result = json_decode($result,true); 

echo $lat = $result['results'][0]['location']['lat']; 
echo $lng = $result['results'][0]['location']['lng']; 

注: -

1.根據您目前的格式,也可以得到數據如下圖所示: -

echo $lat = $result->results[0]->location->lat; //using object approach 
echo $lng = $result->results[0]->location->lng; //using object approach 

2.使用foreach()讓所有lat-lng記錄。

foreach($result->results as $val) { //if using stdclass array 
    echo "Latitude = ".$val->location->lat; 
    echo "Longitude = ".$val->location->lng; 
} 

或者: -

foreach($result['results'] as $val) {//if using normal array 
    echo "Latitude = ".$val['location']['lat']; 
    echo "Longitude = ".$val['location']['lng']; 
} 
+1

知道它是一些簡單的我失蹤了......這就是我一直在尋找...謝謝! – user756659

+0

@ user756659很高興幫助你:) :) –

1

使用第二個參數爲轉換所有對象數組爲真。否則,你可以得到這樣的值。

$result->results[0]->location->lat; 
$result->results[0]->location->lng; 
+1

這是'lng'不''長'。現在+1,因爲你也是正確的。 –

1

您可以從結果存取緯度經度如下

$result = json_decode($result); 
echo $result->results[0]->location->lat; 
echo $result->results[0]->location->lng; 
1

我可以看到,從你的榜樣,你有多個記錄來獲取。使用foreach()打印細節

$result = json_decode($result); 
foreach($result->results as $value) { 
    echo "Lat--",$value->location->lat; //using object 
    echo "Long--",$value->location->lng; 
}