duanjue6575
2017-12-31 10:41
浏览 151

如何将这个Json数据放入Php变量?

I´m having problems with json, I don´t get why my code doesn´t work. I want to put the [secondary_text] => United Kingdom into a php variable, but I´m getting this Notice all the time:

Notice: Trying to get property 'predictions' of non-object in C:.........\CCPSeven.php on line 153

My Code:

  header('Content-Type: application/json');
    $htmlj = file_get_html('https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=*****&input=London&language=en',false);
    $jsondecode2 = json_decode($htmlj);
        foreach ($jsondecode2 as $jsonforeach2) {
        $Country = ($jsonforeach2->description->structured_formatting->secondary_text);
    }
    print_r($Country);

The Json:

stdClass Object
(
    [predictions] => Array
        (
            [0] => stdClass Object
                (
                    [description] => London, United Kingdom
                    [id] => *****+
                    [matched_substrings] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [length] => 6
                                    [offset] => 0
                                )

                        )

                    [place_id] => ChIJdd4hrwug2EcRmSrV3Vo6llI
                    [reference] => *****
                    [structured_formatting] => stdClass Object
                        (
                            [main_text] => London
                            [main_text_matched_substrings] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [length] => 6
                                            [offset] => 0
                                        )

                                )

                            [secondary_text] => United Kingdom
                        )
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtou9934 2017-12-31 10:51
    已采纳

    You might find it a bit simpler to decode the JSON string to a PHP associative array rather than an object:

    $jsondecode2 = json_decode($htmlj, true);
    $Country = $jsondecode2['predictions'][0]['structured_formatting']['secondary_text'];
    

    The 2nd option on the json_decode function tells it to return an array rather than an object. More info here: http://php.net/manual/en/function.json-decode.php

    In your sample code, looks like maybe you've missed off the ['predictions'] bit in your foreach loop? Here are two examples for Object and Array style:

    // Object style
    $jsondecode2 = json_decode(file_get_contents($htmlj));
    foreach ($jsondecode2->predictions as $jsonforeach2) {
        $Country = $jsonforeach2->structured_formatting->secondary_text;
        print PHP_EOL . "Object style: Country: " . $Country;
    }
    
    // Associative array style
    $jsondecode2 = json_decode(file_get_contents($htmlj), true);
    foreach ($jsondecode2['predictions'] as $jsonforeach2) {
        $Country = $jsonforeach2['structured_formatting']['secondary_text'];
        print PHP_EOL . "Array style: Country: " . $Country;
    }
    
    点赞 评论

相关推荐 更多相似问题