繁体   English   中英

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

[英]Select certain parts from last.fm JSON response in PHP

我正在从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 object(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]
              ...

这是解析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埃文斯(吉他,键盘,伴奏),亚当·克莱顿(低音)和小拉里·穆伦(鼓,打击乐)。乐队是有史以来销量最大的另类摇滚乐,截至2008年,该专辑在全球已售出1.4亿张专辑他们赢得了22项格莱美奖,并于2005年入选摇滚名人堂。截至2009年,他们发行了12张专辑和58张单曲。

在Last.fm上了解有关U2的更多信息。

演示

暂无
暂无

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

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