代码之家  ›  专栏  ›  技术社区  ›  user756659

php解析json数据

  •  3
  • user756659  · 技术社区  · 9 年前

    从来没有这样做过,并且对格式以及如何从中访问特定变量感到困惑。

    结果:

    $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']; 等等

    4 回复  |  直到 9 年前
        1
  •  3
  •   Death-is-the-real-truth    9 年前

    你必须使用 true 作为中的第二个参数 json_decode 因此,输出将是正常的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'];
    }
    
        2
  •  2
  •   Subodh Ghulaxe    9 年前

    $result = json_decode($result);
    echo $result->results[0]->location->lat;
    echo $result->results[0]->location->lng;
    
        3
  •  1
  •   Duke    9 年前

    从您的示例中可以看出,您需要获取多条记录。使用 foreach() 打印详细信息

    $result = json_decode($result);
    foreach($result->results as $value) {
       echo "Lat--",$value->location->lat; //using object
       echo "Long--",$value->location->lng;
    }
    
        4
  •  1
  •   Death-is-the-real-truth    9 年前

    将第二个参数设为true,以将所有对象转换为数组。否则你可以得到这样的值。

    $result->results[0]->location->lat;
    $result->results[0]->location->lng;
    
    推荐文章