2010-02-10 109 views
2

這就是我得到儘可能從進取景器URL(JSON編碼)的字符串:如何解碼這個JSON字符串?

{"updated":1265787927,"id":"http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson","title":"Feed results for \"http://itcapsule.blogspot.com/\"","self":[{"href":"http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson"}],"feed":[{"href":"http://itcapsule.blogspot.com/feeds/posts/default"}]} 

如何使用json_decode我解碼()函數在PHP和獲得最後一個數組元素(「飼料」) ?我用下面的代碼,但沒有運氣

$json = file_get_contents("http://www.google.com/reader/api/0/feed-finder?q=http://itcapsule.blogspot.com/&output=json"); 
$ar = (array)(json_decode($json,true)); 
print_r $ar; 

請幫忙試了一下..

+1

道歉,如果我誤解,但你不想只是$ ar ['feed']? – jasonbar 2010-02-10 08:46:13

+0

PHP中不需要類型轉換。 – 2010-02-10 09:34:15

+0

@Jasonbar, 是的你是對的。我只需要「feed」值。 – Orion 2010-02-10 10:50:48

回答

2
$array = json_decode($json, true); 
$feed = $array['feed']; 

注意json_decode()已經返回,當你與true作爲第二個參數調用它的陣列。

更新:

由於feed在JSON

"feed":[{"href":"http://itcapsule.blogspot.com/feeds/posts/default"}] 

值對象數組中,$array['feed']內容是:

Array 
(
    [0] => Array 
     (
      [href] => http://itcapsule.blogspot.com/feeds/posts/default 
     ) 
) 

要獲得URL你必須使用$array['feed'][0]['href']$feed[0]['href']訪問陣列。

但這是數組的基本處理。也許Arrays documentation可以幫助你。

+0

@Felix, 現在我得到的結果是「Array」:( – Orion 2010-02-10 10:49:32

+0

@Raveesh:查看我的更新回答。 – 2010-02-10 11:08:19

+0

Thanks dude。It works !!你是一個冠軍! – Orion 2010-02-10 11:13:45