在同一个div中解析嵌套的json

时间:2012-11-12 18:22:27

标签: php ajax json parsing

  

可能重复:
  Nested json to php search listing

我正在尝试解析一些JSON,我目前在一个页面上解析了所有内容,但是它放在一个页面上的信息太多我希望能够通过列出父对象“church”来过滤它,然后一旦点击了父母,我就会想要列出子(嵌套)对象“轨迹”(基本上是通过JSON过滤),(想想你点击艺术家的iPod(就是“教堂”)然后是专辑(这是“专辑”然后是一首曲目(在json中是“曲目”)

那么如何让父对象成为解析下一级JSON的链接?

您可以访问http://ggcc.tv/JSON_Parsers/Music.php查看我目前所拥有的内容

请帮助,谢谢。

PHP:     http://www.godsgypsychristianchurch.net/music.json“;         $ content = file_get_contents($ filepath);         $ json = json_decode($ content,true);

    $count = 0;
    foreach ($json['rows'] as $row) {
        ++$count;

        echo "<h1>Row: {$count}</h1>";
        echo "Album: {$row[doc]['album']}<br />";
        echo "Church: {$row[doc]['church']}<br />";
        echo "Description: {$row[doc]['des']}<br />";
        echo "<img src=\"{$row['doc']['artwork']}\" alt=\"my image \" width=\"250\" /><br /><br />";

        $songCount = 0;
        foreach ($row['doc']['tracks'] as $song) {
            ++$songCount;
            echo $song['name'] . ' - ';

            $songUrl = $row['doc']['baseurl'] . urldecode($song['url']);
            echo "<a href=\"{$songUrl}\">{$songUrl}</a><br />";
        }
        echo '<br /><br />';
    }

    exit;
?>

JSON(http://www.godsgypsychristianchurch.net/music.json):

{"total_rows":1,"offset":0,"rows":[

              {"id":"All Things Are Possible",
"key":"All Things Are Possible",
"doc":{"_id":"All Things Are Possible",
"album":"All Things Are Possible",
"artwork":"http://godsgypsychristianchurch.net/music_artwork/DEFAULT_COVER2.png",
"baseurl":"http://www.godsgypsychristianchurch.net/music",
"church":"Atlanta GA",
"cityartwork":"http://www.ggcc.tv/LogoNE.png",
"des":"All Things Are Possible from the Atlanta GA Church, Pastor Nick White",
"tracks":[
    {"name":"1 Intro",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/01%20Intro.mp3"},

            {"name":"2 Wo si O Drom",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/02%20Wo%20si%20O%20drom.mp3"},

            {"name":"3 Nas Murrgo Shov",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/03%20Nas%20murrgo%20shov.mp3"},

            {"name":"4 To Cho Vos",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/04%20To%20cho%20vos.mp3"},

            {"name":"5 Tu Son Kai Sastridas",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/05%20Tu%20son%20kai%20sastridas.mp3"},

            {"name":"6 Now I Am Strong",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/06%20Now%20I%20am%20strong.mp3"},

            {"name":"7 Sorr Nevee",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/07%20Zorr%20nevee.mp3"},

            {"name":"8 Preaching",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/08%20Preaching.mp3"},

            {"name":"9 Arkadyan Amey",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/09%20Arkadyan%20amey.mp3"},

            {"name":"10 O Christo Ka Wudarr",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/10%20O%20Christo%20ka%20wudarr.mp3"},

            {"name":"11 Eloi, Eloi",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/11%20Eloi%2C%20Eloi.mp3"},

            {"name":"12 Amadow Dell",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/12%20Amadow%20Dell.mp3"},

            {"name":"13 Sastiar Amey Devla",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/13%20Sastiar%20amey%20Devla.mp3"},

            {"name":"14 Tu Skepeese",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/14%20Tu%20skepeese.mp3"},

            {"name":"15 Dov Ma Godgee",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/15%20Dov%20ma%20godgee.mp3"},

            {"name":"16 The Lord is my strength",
        "url":"/Atlanta%20GA/All%20things%20are%20possible/16%20The%20Lors%20is%20my%20strength.mp3"}
  ]}},

1 个答案:

答案 0 :(得分:1)

假设您想要将教堂与他们的音乐联系起来,首先您将循环播放教堂名称,将它们放入阵列,重复删除,打印出来,将它们链接起来

$churches = array();

foreach ($json['rows'] as $row)
{
    if (!in_array($row[doc]['church'], $churches))
        $churches[] = $row[doc]['church'];
}

foreach ($churches as $church)
{
    print '<a href="getchurchmusic.php?churchname=' . urlencode($church) . '><br />' . htmlentities($church) . '</a>'; }
}

然后在getchurchmusic.php做你现在正在做的事情,但过滤教会

    foreach ($json['rows'] as $row)
    {
            if ($_GET['churchname'] == $row[doc]['church'])
            {
                    /* print out songs or whatever */

            }
    }

这没有经过测试,这样效率不高 - 你应该将所有这些数据放在数据库中,缓存文件等,并且不是XSS免费的,只是粗略的代码给你一个起点