我如何显示来自api的json数组数据

时间:2018-10-23 01:20:50

标签: php arrays json api

以下是我的数据从API获取,我尝试显示shopId,Shopname和shopcode但失败了,有谁能帮助我解决此问题?我正在使用PHP编写脚本并获取API数据。下面是我的脚本以获取API数据。

   <?php      
    $get = file_get_contents("http://api.meirenji.cn/api/xxxxxxxxxxx");
    $json = json_decode($get);

    $shop_id = $json->content->shopId;
    echo "Shop ID : ".$shop_id."";
    ?>

下面是我从API获取的数据,它具有多个数组数据,我需要获取数据shopId,Shopname,shopcode

stdClass Object
(
    [status] => 0
    [msg] => succes
    [content] => Array
        (
            [0] => stdClass Object
                (
                    [shopId] => 6674
                    [shopname] => SP
                    [shopcode] => 38862185
                    [type] => shop
                    [flag] => 1
                )

            [1] => stdClass Object
                (
                    [shopId] => 6498
                    [shopname] => Adidas
                    [shopcode] => 74812597
                    [type] => shop
                    [flag] => 1
                )

            [2] => stdClass Object
                (
                    [shopId] => 6498
                    [shopname] => Nike 
                    [shopcode] => 98741852
                    [type] => shop
                    [flag] => 1
                )

            [3] => stdClass Object
                (
                    [shopId] => 6501
                    [shopname] => Puma
                    [shopcode] => 13847915
                    [type] => shop
                    [flag] => 1
                )

            [4] => stdClass Object
                (
                    [shopId] => 6509
                    [shopname] => Adidas NEO
                    [shopcode] => 26700485
                    [type] => shop
                    [flag] => 1
                )

            [5] => stdClass Object
                (
                    [shopId] => 6865
                    [shopname] => Testing Unit
                    [shopcode] => 23891935
                    [type] => shop
                    [flag] => 1
                )

            [6] => stdClass Object
                (
                    [shopId] => 9661
                    [shopname] => UNDER ARMOR
                    [shopcode] => 88741294
                    [type] => shop
                    [flag] => 1
                )
        )
)

1 个答案:

答案 0 :(得分:0)

要从响应对象中获取商店名称商店代码,您必须像这样使用{{1} }

foreach()

演示: https://3v4l.org/tWqt7

相关问题