duanjue6575 2017-12-31 10:41
浏览 167
已采纳

如何将这个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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题