2013-07-08 112 views
3

我知道這可能是簡單的,我開始明白我需要如何去做這件事,我意識到這與OO有關,但是因爲我是新的到OO和JSON即時通訊有點問題與此http://api.discogs.com/database/search?q=d&page=1&per_page=5它返回5結果與查詢d JSON格式,所以我解析它的PHP功能,然後嘗試讓我們說,顯示thubmnail的第一個結果這是我的解析代碼:致命錯誤:不能使用類型stdClass作爲數組的對象

$returnData = file_get_contents($queryURL); 
$discogsJSON = json_decode($returnData); 

其中$queryURL =提供的網址。如果我打印$discogsJSON我得到這個:

<pre>stdClass Object 
(
[pagination] => stdClass Object 
    (
     [per_page] => 5 
     [items] => 179465 
     [page] => 1 
     [urls] => stdClass Object 
      (
       [last] => http://api.discogs.com/database/search?q=f&per_page=5&page=35893 
       [next] => http://api.discogs.com/database/search?q=f&per_page=5&page=2 
      ) 

     [pages] => 35893 
    ) 

[results] => Array 
    (
     [0] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Free Jazz 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg 
       [format] => Array 
        (
         [0] => Vinyl 
         [1] => LP 
         [2] => Album 
        ) 

       [country] => Switzerland 
       [title] => Steve Lacy - Clinkers 
       [uri] => /Steve-Lacy-Clinkers/master/54331 
       [label] => Array 
        (
         [0] => Hat Hut Records 
        ) 

       [catno] => F 
       [year] => 1978 
       [genre] => Array 
        (
         [0] => Jazz 
        ) 

       [resource_url] => http://api.discogs.com/masters/54331 
       [type] => master 
       [id] => 54331 
      ) 

     [1] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Free Jazz 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg 
       [format] => Array 
        (
         [0] => Vinyl 
         [1] => LP 
         [2] => Album 
        ) 

       [country] => Switzerland 
       [title] => Steve Lacy - Clinkers 
       [uri] => /Steve-Lacy-Clinkers/release/1672955 
       [label] => Array 
        (
         [0] => Hat Hut Records 
        ) 

       [catno] => F 
       [year] => 1978 
       [genre] => Array 
        (
         [0] => Jazz 
        ) 

       [resource_url] => http://api.discogs.com/releases/1672955 
       [type] => release 
       [id] => 1672955 
      ) 

     [2] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Alternative Rock 
         [1] => Goth Rock 
         [2] => Heavy Metal 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-2785283-1300918365.jpeg 
       [format] => Array 
        (
         [0] => CD 
         [1] => Compilation 
         [2] => Unofficial Release 
        ) 

       [country] => Russia 
       [title] => HIM (2) - Overload Hits 
       [uri] => /HIM-Overload-Hits/release/2785283 
       [label] => Array 
        (
         [0] => FullHouse Records 
        ) 

       [catno] => F????? 
       [year] => 2003 
       [genre] => Array 
        (
         [0] => Rock 
        ) 

       [resource_url] => http://api.discogs.com/releases/2785283 
       [type] => release 
       [id] => 2785283 
      ) 

     [3] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Drone 
         [1] => Ambient 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-3108621-1316178067.jpeg 
       [format] => Array 
        (
         [0] => CDr 
         [1] => Album 
         [2] => Limited Edition 
        ) 

       [country] => Russia 
       [title] => Solar Ocean - Twillight October 
       [uri] => /Solar-Ocean-Twillight-October/release/3108621 
       [label] => Array 
        (
         [0] => Necrophone 
        ) 

       [catno] => F 
       [year] => 2010 
       [genre] => Array 
        (
         [0] => Electronic 
        ) 

       [resource_url] => http://api.discogs.com/releases/3108621 
       [type] => release 
       [id] => 3108621 
      ) 

     [4] => stdClass Object 
      (
       [style] => Array 
        (
         [0] => Techno 
         [1] => Electro 
         [2] => Tech House 
         [3] => Minimal 
        ) 

       [thumb] => http://api.discogs.com/image/R-90-699054-1161976299.jpeg 
       [format] => Array 
        (
         [0] => Vinyl 
         [1] => 12" 
         [2] => Box Set 
         [3] => Compilation 
         [4] => Limited Edition 
        ) 

       [country] => Germany 
       [title] => Various - Cocoon Compilation F 
       [uri] => /Various-Cocoon-Compilation-F/master/33655 
       [label] => Array 
        (
         [0] => Cocoon Recordings 
         [1] => Cocoon Compilation 
        ) 

       [catno] => COR LP 011 
       [year] => 2006 
       [genre] => Array 
        (
         [0] => Electronic 
        ) 

       [resource_url] => http://api.discogs.com/masters/33655 
       [type] => master 
       [id] => 33655 
      ) 

    ) 

) 
</pre> 

這是完全正確的,我可以看到我想要的數據,但是當我試圖訪問(可以說第一的縮略圖),像這樣:

echo $discogsJSON['results']['0']['thumb']; 

我留下:Fatal error: Cannot use object of type stdClass as array

我從這裏假設有足夠的信息來走下車來建構正確的echo語句,但任何人都可以向我解釋了OO理論正確的方式來獲得背後數據出來了?

回答

14

$discogsJSON不是一個數組,而是一個stdClass對象。結果是此對象的屬性。

這應該做你正在尋找。

echo $discogsJSON->results['0']->thumb; 

但是,它可能會更方便易到true標誌添加到json_decode以確保返回的值是一個關聯數組。

$discogsJSON = json_decode($returnData, true); 
+1

你先生是這周我在互聯網上faovrite人<3 – Nick

+0

感謝的第二個參數,使陣列關聯您!如果它滿足您的需求,請接受答案。 – apartridge

+0

6分鐘去 – Nick

1

您需要通過添加真正json_decode

$discogsJSON = json_decode($returnData, true);

相關問題