2010-02-10 106 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