2016-12-06 118 views
-1

例如:如何從php獲取json元素?

{ 
    "destination_addresses" : [ "Milano, Italia" ], 
    "origin_addresses" : [ "Padova PD, Italia" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "246 km", 
        "value" : 246492 
       }, 
       "duration" : { 
        "text" : "2 ore 36 min", 
        "value" : 9388 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

我想距離在這種情況下246公里 感謝您的答覆..我不知道....

FROM:https://maps.googleapis.com/maps/api/distancematrix/json?origins=Padova&destinations=Milano&language=it-IT&key=API_KEY

+0

這裏需要更多細節。這是一個外部文件,變量。把你到目前爲止的PHP代碼。 –

+0

更新代碼.... –

+0

我從捲曲中獲得json。 –

回答

1

你可以用json_decode將它轉換爲array這樣的:

<?php 
$json = json_decode('{ 
    "destination_addresses" : [ "Milano, Italia" ], 
    "origin_addresses" : [ "Padova PD, Italia" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "246 km", 
        "value" : 246492 
       }, 
       "duration" : { 
        "text" : "2 ore 36 min", 
        "value" : 9388 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
}',true); 

var_dump($json); 
?> 

結果是:

array(4) { 
    ["destination_addresses"]=> 
    array(1) { 
    [0]=> 
    string(14) "Milano, Italia" 
    } 
    ["origin_addresses"]=> 
    array(1) { 
    [0]=> 
    string(17) "Padova PD, Italia" 
    } 
    ["rows"]=> 
    array(1) { 
    [0]=> 
    array(1) { 
     ["elements"]=> 
     array(1) { 
     [0]=> 
     array(3) { 
      ["distance"]=> 
      array(2) { 
      ["text"]=> 
      string(6) "246 km" 
      ["value"]=> 
      int(246492) 
      } 
      ["duration"]=> 
      array(2) { 
      ["text"]=> 
      string(12) "2 ore 36 min" 
      ["value"]=> 
      int(9388) 
      } 
      ["status"]=> 
      string(2) "OK" 
     } 
     } 
    } 
    } 
    ["status"]=> 
    string(2) "OK" 
} 

爲了只得到您的km,你可以事後這樣做:

<?php 
    echo $json['rows']['0']['elements']['0']['distance']['text']; 
?> 
1

如果手中的JSON是您回收的唯一數據,您可以使用json_decode()創建array()

$json = '{"destination_addresses":["Milano, Italia"],"origin_addresses":["Padova PD, Italia"],"rows":[{"elements":[{"distance":{"text":"246 km","value":246492},"duration":{"text":"2 ore 36 min","value":9388},"status":"OK"}]}],"status":"OK"}'; 

$jsonArray = json_decode($json, true); // get array of json 

var_dump($jsonArray); 

導致此:

array(4) { 
    ["destination_addresses"] => array(1) { 
     [0] => string(14) "Milano, Italia" 
    }, 
    ["origin_addresses"] => array(1) { 
     [0] => string(17) "Padova PD, Italia" 
    }, 
    ["rows"] => array(1) { 
     [0] => array(1) { 
      ["elements"] => array(1) { 
       [0] => array(3) { 
        ["distance"] => array(2) { 
         ["text"] => string(6) "246 km", 
         ["value"] => int(246492) 
        }, 
        ["duration"] => array(2) { 
         ["text"] => string(12) "2 ore 36 min", 
         ["value"] => int(9388) 
        }, 
        ["status"] => string(2) "OK" 
       } 
      } 

之後,你可以做到這一點,以獲得實際的距離:那裏的['0']的是因爲:

print_r($jsonArray['rows']['0']['elements']['0']['distance']['text']); 

// 246 km 

注意如果查看print_r($jsonArray);,您可以看到這些是已轉換的JSON格式文本的一部分。