【问题标题】:How to get element from json with php? [duplicate]如何使用 php 从 json 中获取元素? [复制]
【发布时间】:2016-12-07 06:15:09
【问题描述】:

例如:

 {
   "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 公里的距离 谢谢回复..我不知道....

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

【问题讨论】:

  • 您需要更多详细信息。这是一个外部文件吗,变量。把你到目前为止的 PHP 代码贴上来。
  • 更新代码....
  • 我从 curl 获取 json。

标签: php 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'];
?>

【讨论】:

    【解决方案2】:

    如果那段 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"
                    }
                }
    

    然后你可以这样做以获得实际距离:

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

    注意['0'] 存在,因为如果查看print_r($jsonArray);,您可以看到这些是转换后的 JSON 格式文本的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多