使用 PHP 显示 JSON 数据中的多个嵌套项

我正在尝试从 PHP 中的 JSON 文件中解析一些数据。现在,我让它在本地工作,但是当我把它放到网站上时,我收到了警告错误( Warning: Invalid argument supplied for foreach() )


请帮忙,我对此很陌生,并且绕着圈子阅读并试图弄清楚。


基本上,我想列出所有“艺术家”并包括他们的姓名、UUID 和图像。


JSON


{

    "items": [

        {

            "title": null,

            "itemType": "artist",

            "moreTrackingTitle": "All Artists",

            "items": [

                {

                    "Artist": "Artist One",

                    "UUID": "364226",

                    "Image": "http://theurl.com",

                },

                {

                    "Artist": "Artist Two",

                    "UUID": "1513513",

                    "Image": "http://theurl.com",

                },

                {

                    "Artist": "Artist Three",

                    "UUID": "214141",

                    "Image": "http://theurl.com",

                }

            ]

        },

    ],

    "nextPageToken": null

}

PHP


$request = wp_remote_get( 'http://EXTERNAL-JSON-FILE' );

    $body = wp_remote_retrieve_body( $request );


    $data =json_decode($body, true);


    foreach($data as $k1 => $v1) {

        foreach($v1 as $k2 => $v2) {

            foreach($v2 as $k3 => $v3) {

                foreach($v3 as $artist) {

                    echo $artist['Artist'];

                    echo $artist['UUID'];

                    echo $artist['Image'];

                }

            }

        }

    }


凤凰求蛊
浏览 131回答 1
1回答

BIG阳

无效的 JSONjson_decode失败(无提示)并返回,null因为这不是有效的 JSON 文档。JSON 不允许在数组的最后一项或对象的最后列出的属性上使用逗号。你从哪里得到这个 JSON 文档?这是有效的 Javascript,但不是有效的 JSON。违规行是:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Artist": "Artist One",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UUID": "364226",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Image": "http://theurl.com", <-- remove this in the other objects too&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },和&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; }, <-- remove this&nbsp; &nbsp; ],&nbsp; &nbsp; "nextPageToken": null}希望你拥有这个“外部 json 文件”并且可以编辑它,否则就没有办法解决它。用json linter检查你的 json 文件。错误使用嵌套foreach循环要得到你想要的东西(假设每个项目都是“艺术家”):foreach($data["items"] as $item){&nbsp; &nbsp; foreach($item["items"] as $artist){&nbsp; &nbsp; &nbsp; &nbsp; echo $artist["Artist"];&nbsp; &nbsp; &nbsp; &nbsp; echo $artist["UUID"];&nbsp; &nbsp; &nbsp; &nbsp; echo $artist["Image"];&nbsp; &nbsp; }}你不应该foreachJSON 对象的每个属性,只有那些是可迭代的。附录这是固定的 json 文件,删除了有问题的逗号:{&nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title": null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "itemType": "artist",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "moreTrackingTitle": "All Artists",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Artist": "Artist One",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UUID": "364226",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Image": "http://theurl.com"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Artist": "Artist Two",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UUID": "1513513",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Image": "http://theurl.com"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Artist": "Artist Three",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UUID": "214141",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Image": "http://theurl.com"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ],&nbsp; &nbsp; "nextPageToken": null}要强制 PHP 在读取无效的 JSON 字符串时抛出异常而不是返回 null,请将 JSON_THROW_ON_ERROR 作为第四个参数传递:$data = json_decode(file_get_contents("data.json"), true, 512, JSON_THROW_ON_ERROR);
打开App,查看更多内容
随时随地看视频慕课网APP