【问题标题】:How to get the value of distance from the following JSON object?如何从以下 JSON 对象中获取距离值?
【发布时间】:2016-05-19 13:26:50
【问题描述】:

我需要从 PHP 中的以下 JSON 对象中获取值 361714。 JSON 是使用谷歌距离矩阵 api 获得的。
链接: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;
        }

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

【问题讨论】:

  • 到目前为止,您的代码看起来如何,您到底在努力解决什么问题?
  • json_decode开头

标签: php json


【解决方案1】:

尝试:

$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

【讨论】:

  • json_decode 不返回一个 stdClass,你需要使用 '->' 像 $details->rows 吗?
  • "rows" 也是一个数组
  • @DevNiels 如果您将 'true' 作为第二个参数传递给 json_decode(),则不会返回关联数组。
  • @Tudor Constantin 还是同样的错误:未定义元素。
  • @BobbyJack - 感谢您的观察,我已经更新了答案。
【解决方案2】:

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

$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字符串并get和object; 第二:逐个访问对象,首先是行,然后是行内的元素;

【讨论】:

    【解决方案3】:

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

    $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);
    

    【讨论】:

      【解决方案4】:

      使用 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        相关资源
        最近更新 更多