如何从以下JSON对象获取距离值?

时间:2016-05-19 13:26:50

标签: php json

我需要从PHP中的以下JSON对象获取值 361714 。使用谷歌距离矩阵api获得JSON 链接:https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=AIzaSyDVfbt8uCHfFTehDjDS-z_XfYF1O-lLDAE

{
   "destination_addresses" : [ "New York, NY, USA" ],
   "origin_addresses" : [ "Washington, DC, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "225 mi",
                  "value" : 361714
               },
               "duration" : {
                  "text" : "3 hours 49 mins",
                  "value" : 13767
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"

我试过这个:

for($f = 0; $f<count($address); $f++)
        {
            $url = "http://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=" . $for_cust_add . "&destinations=" . $for_add[$f] . "&key=AIzaSyDVfbt8uCHfFTehDjDS-z_XfYF1O-lLDAE";
            $json = file_get_contents($url);
            $details = json_decode($json, true);
            $distance = $details['rows']['elements']['distance']['value'];
            $distance_array[$f] = $distance;
        }

它给出了错误:未定义的元素。

4 个答案:

答案 0 :(得分:2)

尝试:

$distance = $details['rows'][0]['elements'][0]['distance']['value'];

$details['rows']['elements']是一个数组,您需要它的第一个元素,因此添加了[0]

更新 - 问题中json的完整概念证明:

<?php
$json_file = '
{
   "destination_addresses" : [ "New York, NY, USA" ],
   "origin_addresses" : [ "Washington, DC, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "225 mi",
                  "value" : 361714
               },
               "duration" : {
                  "text" : "3 hours 49 mins",
                  "value" : 13767
               },
               "status" : "OK"
            }
         ]
      }
   ]
}';

$details = json_decode($json_file, true);
$distance = $details['rows'][0]['elements'][0]['distance']['value'];
print_r($distance);
?>

以上代码打印361714

答案 1 :(得分:0)

元素和距离在一个额外的数组中,这就是为什么你不能以你尝试的方式访问它们的原因:

$json = '{ "destination_addresses" : [ "New York, NY, USA" ],
           "origin_addresses" : [ "Washington, DC, USA" ],
           "rows" : [
              {
                 "elements" : [
                    {
                       "distance" : {
                          "text" : "225 mi",
                          "value" : 361714
                       },
                       "duration" : {
                          "text" : "3 hours 49 mins",
                          "value" : 13767
                       },
                       "status" : "OK"
                    }
                 ]
              }
           ],
           "status" : "OK" }';

$data = json_decode($json);

foreach ($data->rows as $row) {
    foreach ($row->elements as $element) {
        var_dump($element->distance->value);
    }
}

1st:解码json字符串并获取和对象; 第二步:逐个访问对象,首先是行,然后是行内的元素;

答案 2 :(得分:0)

如果您只想查询返回的第一个条目,请尝试:

$distance = $details['rows'][0]['elements'][0]['distance']['value'];

如果你想检查所有元素:

foreach($row in $details['rows'])
  foreach($element in $row['elements'])
    $distance = $element['distance']['value'];

将来,使用 print_r 进行调试;然后你可以看到 json_decode 返回的实际结构是什么样的。

print_r($details);

答案 3 :(得分:0)

使用json_decode()

//Enter your code here, enjoy!
$json_file = '
{
   "destination_addresses" : [ "New York, NY, USA" ],
   "origin_addresses" : [ "Washington, DC, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "225 mi",
                  "value" : 361714
               },
               "duration" : {
                  "text" : "3 hours 49 mins",
                  "value" : 13767
               },
               "status" : "OK"
            }
         ]
      }
   ]
}';

// convert the string to a json object
$jsonObject = json_decode($json_file);
// read the distance object
$distance = $jsonObject->rows[0]->elements[0]->distance;
print_r($distance);
echo 'text= ' . $distance->text . ', value = ' . $distance->value;