简体   繁体   English

遍历JSON对象属性

[英]Looping through JSON object properties

I'm totally newbie what comes to programming so i'm not even quite sure if my terms are right but i would like to get some hints and tips what is best practice to loop through JSON object? 我完全是编程方面的新手,所以我什至不确定我的术语是否正确,但是我想获得一些提示和技巧,这是遍历JSON对象的最佳实践是什么? Let's say i want all game names from following JSON print_r output. 假设我想要来自以下JSON print_r输出的所有游戏名称。

stdClass Object
(
    [_total] => 555
    [_links] => stdClass Object
        (
            [self] => https://api.twitch.tv/kraken/games/top?limit=2&offset=0
            [next] => https://api.twitch.tv/kraken/games/top?limit=2&offset=2
        )

    [top] => Array
        (
            [0] => stdClass Object
                (
                    [viewers] => 86386
                    [channels] => 1159
                    [game] => stdClass Object
                        (
                            [name] => League of Legends
                            [_id] => 21779
                            [giantbomb_id] => 24024
                            [box] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-52x72.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-136x190.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg
                                )

                            [logo] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-60x36.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-120x72.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-240x144.jpg
                                )

                            [_links] => stdClass Object
                                (
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [viewers] => 17288
                    [channels] => 162
                    [game] => stdClass Object
                        (
                            [name] => Hearthstone: Heroes of Warcraft
                            [_id] => 138585
                            [giantbomb_id] => 42033
                            [box] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-52x72.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-136x190.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-272x380.jpg
                                )

                            [logo] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-60x36.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-120x72.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-240x144.jpg
                                )

                            [_links] => stdClass Object
                                (
                                )

                        )

                )

        )

)

I can access single line (not sure if this is best practise either): 我可以访问单行(不确定这是否也是最佳实践):

$OBJ->method()->top[0]->game->name;

But i'm more than clueless how to loop through all game names. 但是我绝不知道如何遍历所有游戏名称。

Any help much appreciated! 任何帮助,不胜感激!

The "name"s are accessed using $OBJ->top[0]->game->name etc... So just foreach over the "top" array: 使用$OBJ->top[0]->game->name等访问“名称” ...因此,只需foreach “ top”数组即可:

foreach($OBJ->top as $object) {
    echo $object->game->name;
}

Create an empty array, loop the objects top array and fill your empty array: 创建一个空数组,将对象循环到顶部数组并填充您的空数组:

$allgames=array();
foreach($OBJ->method()->top as $ob){

    $allgames[] = $ob->game->name;
}

When you load your JSON string into PHP you can use: 将JSON字符串加载到PHP中时,可以使用:

json_decode($string_of_json, true);

The true flag will load it into an array you can loop through using, for example, foreach . true标志会将其加载到可以使用例如foreach循环进行遍历的数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM