PHP打印JSON值

时间:2014-05-20 17:59:58

标签: php json

我有一个JSON数组,我希望能够深入到较低级别并打印该值。当我达到[0](或[n])时,会出现问题。例如,我有以下输出,我想打印第一个联盟的game key

这就是我试图打印它的方式

HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))

但是我一直收到这个错误:

Cannot use object of type stdClass as array

当我以递增方式执行此操作时,此命令似乎失败(因此我假设我没有正确索引):

$user->fantasy_content->users[0]

这是输出:

stdClass Object
(
    [fantasy_content] => stdClass Object
        (
            [xml:lang] => en-US
            [yahoo:uri] => /fantasy/v2/users;use_login=1/games
            [users] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [user] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
                                        )

                                    [1] => stdClass Object
                                        (
                                            [games] => stdClass Object
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [game] => Array
                                                                (
                                                                    [0] => stdClass Object
                                                                        (
                                                                            [game_key] => 147
                                                                            [game_id] => 147
                                                                            [name] => Baseball
                                                                            [code] => mlb
                                                                            [type] => full
                                                                            [url] => http://baseball.fantasysports.yahoo.com/b1
                                                                            [season] => 2006
                                                                        )

                                                                )

                                                        )

                                                    [count] => 1
                                                )

                                        )

                                )

                        )

                    [count] => 1
                )

            [time] => 52.390813827515ms
            [copyright] => Data provided by Yahoo! and STATS, LLC
            [refresh_rate] => 60
        )

)

2 个答案:

答案 0 :(得分:0)

您可以将stdClass对象转换为数组,方法如下:

<?php

$array = (array) $myObject;

echo json_encode($array);

您也可以投射内联:

<?php

echo json_encode((array) $object);

答案 1 :(得分:0)

对于对象,您必须使用->语法,如果键/属性名称是数字或具有其他特殊字符,则需要使用$object->{'0'}语法。

可以使用以下方法检索game_key

$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;