【问题标题】:php parsing json dataphp解析json数据
【发布时间】:2017-10-10 04:52:54
【问题描述】:

不必这样做,也不必对格式以及如何从中访问特定变量感到困惑。

结果:

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

        )

)

如何访问 lat 和 lng 值?我很困惑,因为里面混合了对象和数组。我从来没有以这种方式与 php 一起使用过 json ......它是一个以 json 格式返回的地理编码 api。

我基本上想做$lat = $result['results'][0]['location']['lat'];之类的事情。

【问题讨论】:

  • 使用类型转换,将其转换为对象

标签: php json


【解决方案1】:

您必须在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'];
}

【讨论】:

  • 知道它必须是我所缺少的一些简单的东西......这就是我正在寻找的......谢谢!
  • @user756659 很高兴为您提供帮助:):)
【解决方案2】:

您可以从结果中访问 lat lng,如下所示

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

【讨论】:

    【解决方案3】:

    我可以从您的示例中看到您有多个记录要获取。使用foreach() 打印详细信息

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

    【讨论】:

      【解决方案4】:

      使用第二个参数为 true 将所有对象转换为数组。否则,您可以获得这样的值。

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

      【讨论】:

      • 它是 lng 而不是 long。现在 +1,因为你也是正确的。
      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 2017-09-01
      相关资源
      最近更新 更多