【问题标题】:Fatal error: Cannot use object of type stdClass as array致命错误:不能将 stdClass 类型的对象用作数组
【发布时间】:2013-07-05 06:44:52
【问题描述】:

我知道这可能很简单,我开始明白我需要如何去做,我意识到这与 OO 有关,但是因为我是 OO 的新手,而 json 我遇到了一些问题有了这个http://api.discogs.com/database/search?q=d&page=1&per_page=5,它返回5个结果,查询d以json格式返回,所以我用php函数解析它,然后尝试让我们说显示第一个结果的缩略图这是我的解析代码:

$returnData = file_get_contents($queryURL);
$discogsJSON = json_decode($returnData);

其中$queryURL = 提供的网址。如果我打印 $discogsJSON 我得到这个:

<pre>stdClass Object
(
[pagination] => stdClass Object
    (
        [per_page] => 5
        [items] => 179465
        [page] => 1
        [urls] => stdClass Object
            (
                [last] => http://api.discogs.com/database/search?q=f&per_page=5&page=35893
                [next] => http://api.discogs.com/database/search?q=f&per_page=5&page=2
            )

        [pages] => 35893
    )

[results] => Array
    (
        [0] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Free Jazz
                    )

                [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg
                [format] => Array
                    (
                        [0] => Vinyl
                        [1] => LP
                        [2] => Album
                    )

                [country] => Switzerland
                [title] => Steve Lacy - Clinkers
                [uri] => /Steve-Lacy-Clinkers/master/54331
                [label] => Array
                    (
                        [0] => Hat Hut Records
                    )

                [catno] => F
                [year] => 1978
                [genre] => Array
                    (
                        [0] => Jazz
                    )

                [resource_url] => http://api.discogs.com/masters/54331
                [type] => master
                [id] => 54331
            )

        [1] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Free Jazz
                    )

                [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg
                [format] => Array
                    (
                        [0] => Vinyl
                        [1] => LP
                        [2] => Album
                    )

                [country] => Switzerland
                [title] => Steve Lacy - Clinkers
                [uri] => /Steve-Lacy-Clinkers/release/1672955
                [label] => Array
                    (
                        [0] => Hat Hut Records
                    )

                [catno] => F
                [year] => 1978
                [genre] => Array
                    (
                        [0] => Jazz
                    )

                [resource_url] => http://api.discogs.com/releases/1672955
                [type] => release
                [id] => 1672955
            )

        [2] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Alternative Rock
                        [1] => Goth Rock
                        [2] => Heavy Metal
                    )

                [thumb] => http://api.discogs.com/image/R-90-2785283-1300918365.jpeg
                [format] => Array
                    (
                        [0] => CD
                        [1] => Compilation
                        [2] => Unofficial Release
                    )

                [country] => Russia
                [title] => HIM (2) - Overload Hits
                [uri] => /HIM-Overload-Hits/release/2785283
                [label] => Array
                    (
                        [0] => FullHouse Records
                    )

                [catno] => F?????
                [year] => 2003
                [genre] => Array
                    (
                        [0] => Rock
                    )

                [resource_url] => http://api.discogs.com/releases/2785283
                [type] => release
                [id] => 2785283
            )

        [3] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Drone
                        [1] => Ambient
                    )

                [thumb] => http://api.discogs.com/image/R-90-3108621-1316178067.jpeg
                [format] => Array
                    (
                        [0] => CDr
                        [1] => Album
                        [2] => Limited Edition
                    )

                [country] => Russia
                [title] => Solar Ocean - Twillight October
                [uri] => /Solar-Ocean-Twillight-October/release/3108621
                [label] => Array
                    (
                        [0] => Necrophone
                    )

                [catno] => F
                [year] => 2010
                [genre] => Array
                    (
                        [0] => Electronic
                    )

                [resource_url] => http://api.discogs.com/releases/3108621
                [type] => release
                [id] => 3108621
            )

        [4] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Techno
                        [1] => Electro
                        [2] => Tech House
                        [3] => Minimal
                    )

                [thumb] => http://api.discogs.com/image/R-90-699054-1161976299.jpeg
                [format] => Array
                    (
                        [0] => Vinyl
                        [1] => 12"
                        [2] => Box Set
                        [3] => Compilation
                        [4] => Limited Edition
                    )

                [country] => Germany
                [title] => Various - Cocoon Compilation F
                [uri] => /Various-Cocoon-Compilation-F/master/33655
                [label] => Array
                    (
                        [0] => Cocoon Recordings
                        [1] => Cocoon Compilation
                    )

                [catno] => COR LP 011
                [year] => 2006
                [genre] => Array
                    (
                        [0] => Electronic
                    )

                [resource_url] => http://api.discogs.com/masters/33655
                [type] => master
                [id] => 33655
            )

    )

)
</pre>

这是完全正确的,我可以看到我想要的所有数据,但是当我尝试访问(比如说第一个的缩略图)时:

echo $discogsJSON['results']['0']['thumb'];

我留下了:Fatal error: Cannot use object of type stdClass as array

我想从这里有足够的信息来构建正确的 echo 语句,但是谁能向我解释正确获取数据方法背后的 OO 理论?

【问题讨论】:

    标签: php arrays oop


    【解决方案1】:

    您需要通过添加 true 作为 json_decode

    的第二个参数来使数组具有关联性

    $discogsJSON = json_decode($returnData, true);

    【讨论】:

      【解决方案2】:

      $discogsJSON 不是一个数组,而是一个stdClass 对象。 results 是此对象的一个​​属性。

      这应该可以满足您的需求。

      echo $discogsJSON->results['0']->thumb;
      

      但是,将true 标志添加到json_decode 以确保返回的值是关联数组可能更方便。

      $discogsJSON = json_decode($returnData, true);
      

      【讨论】:

      • 你先生是我本周在互联网上最喜欢的人
      • 谢谢!如果您满意,请接受答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      相关资源
      最近更新 更多