从PHP中的last.fm JSON响应中选择某些部分

时间:2014-01-04 17:12:30

标签: php api foreach last.fm

我正在从last.fm API中检索艺术家信息。以下是U2的示例回复:

{
   "artist" : {
      "stats" : {
         "playcount" : "117896887",
         "listeners" : "3077194"
      },
      "name" : "U2",
      "tags" : {
         "tag" : [
            {
               "url" : "http://www.last.fm/tag/rock",
               "name" : "rock"
            },
            {
               "url" : "http://www.last.fm/tag/classic%20rock",
               "name" : "classic rock"
            },
            {
               "url" : "http://www.last.fm/tag/irish",
               "name" : "irish"
            },
            {
               "url" : "http://www.last.fm/tag/pop",
               "name" : "pop"
            },
            {
               "url" : "http://www.last.fm/tag/alternative",
               "name" : "alternative"
            }
         ]
      },
      "image" : [
         {
            "#text" : "http://userserve-ak.last.fm/serve/34/12369741.jpg",
            "size" : "small"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/64/12369741.jpg",
            "size" : "medium"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/126/12369741.jpg",
            "size" : "large"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/252/12369741.jpg",
            "size" : "extralarge"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/_/12369741/U2.jpg",
            "size" : "mega"
         }
      ],
      "mbid" : "704acdbb-1415-4782-b0b6-0596b8c55e46",
      "similar" : {
         "artist" : [
            {
               "url" : "http://www.last.fm/music/Passengers",
               "name" : "Passengers",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/4826014.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/4826014.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/4826014.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/4826014.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/500/4826014/Passengers+Brian+Eno++U2+as++Du.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Bono",
               "name" : "Bono",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/2511353.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/2511353.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/2511353.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/2511353.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/2511353/Bono.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/R.E.M.",
               "name" : "R.E.M.",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/33444847.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/33444847.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/33444847.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/33444847.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/500/33444847/REM+11.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Coldplay",
               "name" : "Coldplay",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/28914.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/28914.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/28914.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/28914.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/28914/Coldplay.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Bruce+Springsteen",
               "name" : "Bruce Springsteen",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/2326566.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/2326566.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/2326566.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/2326566.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/2326566/Bruce+Springsteen+bruce.jpg",
                     "size" : "mega"
                  }
               ]
            }
         ]
      },
      "streamable" : "1",
      "bandmembers" : {
         "member" : [
            {
               "yearfrom" : "1976",
               "name" : "Bono"
            },
            {
               "yearfrom" : "1976",
               "name" : "The Edge"
            },
            {
               "yearfrom" : "1976",
               "name" : "Adam Clayton"
            },
            {
               "yearfrom" : "1976",
               "name" : "Larry Mullen, Jr."
            },
            {
               "yearfrom" : "1976",
               "name" : "Dick Evans"
            },
            {
               "yearfrom" : "1976",
               "name" : "Ivan McCormick"
            },
            {
               "yearfrom" : "1976",
               "name" : "Peter Martin"
            }
         ]
      },
      "url" : "http://www.last.fm/music/U2",
      "ontour" : "0",
      "bio" : {
         "published" : "Tue, 27 Sep 2011 15:29:59 +0000",
         "yearformed" : "1976",
         "summary" : "\n U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono</a> (real name Paul Hewson) (vocals, guitar, harmonica), The Edge</a> (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton</a> (bass) and Larry Mullen, Jr.</a> (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles. \n\n Read more about U2 on Last.fm</a>.\n \n ",
         "placeformed" : "Dublin, Ireland",
         "formationlist" : {
            "formation" : {
               "yearfrom" : "1976",
               "yearto" : ""
            }
         },
         "content" : "\n U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono</a> (real name Paul Hewson) (vocals, guitar, harmonica), The Edge</a> (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton</a> (bass) and Larry Mullen, Jr.</a> (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles. \n\n Read more about U2 on Last.fm</a>.\n \n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.\n ",
         "links" : {
            "link" : {
               "rel" : "original",
               "href" : "http://www.last.fm/music/U2/+wiki",
               "#text" : ""
            }
         }
      }
   }
}

我只想显示摘要,但我在脚本中选择它时遇到了麻烦。我试过以下

 echo $result->artist->bio->summary; 

$output = $result->artist->bio->summary;
echo '<ul>';
foreach ($bands as $artist) {
            $name  ->name;
                    etc

}
if ($result === NULL) die('Artist not found');

如果有人可以告诉我如何从返回的JSON中选择并显示一个很棒的对象。我已经尝试了所有我能想到或找到的东西

enter code here对象(stdClass的)[1]

public 'artist' => 
    object(stdClass)[2]
      public 'name' => string 'Foo Fighters' (length=12)
      public 'mbid' => string '67f66c07-6e61-4026-ade5-7e782fad3a5d' (length=36)
      public 'bandmembers' => 
        object(stdClass)[3]
          public 'member' => 
            array (size=7)
              ...
      public 'url' => string 'http://www.last.fm/music/Foo+Fighters' (length=37)
      public 'image' => 
        array (size=5)
          0 => 
            object(stdClass)[11]
              ...
          1 => 
            object(stdClass)[12]
              ...
          2 => 
            object(stdClass)[13]
              ...
          3 => 
            object(stdClass)[14]
              ...
          4 => 
            object(stdClass)[15]
              ...
      public 'streamable' => string '1' (length=1)
      public 'ontour' => string '0' (length=1)
      public 'stats' => 
        object(stdClass)[16]
          public 'listeners' => string '3589242' (length=7)
          public 'playcount' => string '155892081' (length=9)
      public 'similar' => 
        object(stdClass)[17]
          public 'artist' => 
            array (size=5)
              ...
      public 'tags' => 
        object(stdClass)[48]
          public 'tag' => 
            array (size=5)
              ...
      public 'bio' => 
        object(stdClass)[54]
          public 'links' => 
            object(stdClass)[55]
              ...
          public 'published' => string 'Fri, 9 Nov 2012 21:02:30 +0000' (length=30)
          public 'summary' => string '
                <a href="http://www.foofighters.com" rel="nofollow">Foo Fighters</a> are an American <a href="http://www.last.fm/tag/rock" class="bbcode_tag" rel="tag">rock</a> band formed by singer/guitarist/drummer <a href="http://www.last.fm/music/Dave+Grohl" class="bbcode_artist">Dave Grohl</a> in 1995 in Seattle, USA. Grohl formed the group as a one-man project after the dissolution of his previous band <a href="http://www.last.fm/music/Nirvana" class="bbcode_artist">Nirvana</a> in 1994. Prior to the '... (length=1384)
          public 'content' => string '
                    <a href="http://www.foofighters.com" rel="nofollow">Foo Fighters</a> are an American <a href="http://www.last.fm/tag/rock" class="bbcode_tag" rel="tag">rock</a> band formed by singer/guitarist/drummer <a href="http://www.last.fm/music/Dave+Grohl" class="bbcode_artist">Dave Grohl</a> in 1995 in Seattle, USA. Grohl formed the group as a one-man project after the dissolution of his previous band <a href="http://www.last.fm/music/Nirvana" class="bbcode_artist">Nirvana</a> in 1994. Prior to '... (length=1514)
          public 'placeformed' => string 'Seattle, USA' (length=12)
          public 'yearformed' => string '1995' (length=4)
          public 'formationlist' => 
            object(stdClass)[57]
              ...

1 个答案:

答案 0 :(得分:0)

以下是解析json数据的方法:

<强> PHP

$decodedJson = json_decode($json);

$artist = $decodedJson->{"artist"};
$bio = $artist->{"bio"};
$summary = $bio->{"summary"};
var_dump($summary);

节目输出

  

string(631)“U2是一支爱尔兰摇滚乐队,成立于1976年   爱尔兰都柏林。乐队成立以来,他们都是由乐团组成的   Bono(真名Paul Hewson)(主唱,吉他,口琴),The   Edge(真名David Evans)(吉他,键盘,伴唱),   Adam Clayton(贝斯)和Larry Mullen,Jr。(鼓,打击乐)。   乐队是有史以来最畅销的另类摇滚乐队,   截至2008年,全球已售出1.4亿张专辑。他们赢了22张   格莱美奖并入选Rock&amp; 2005年成名名人堂。   截至2009年,他们已经发行了12张专辑和58张单曲。

     

在Last.fm上阅读有关U2的更多信息。 “

Demo