如何解码此JSON字符串? [英] how to decode this JSON string?

查看:95
本文介绍了如何解码此JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从提要查找器URL(JSON编码)中作为字符串得到的:

this is what i get as a string from a feed finder url (JSON Encoded):

{"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"}]}

我如何使用php中的json_decode()函数对其进行解码,并获取最后一个数组元素(提要")?我用下面的代码尝试了一下,但是没有运气

How can i decode it using json_decode() function in php and get the last array element ("feed") ? i tried it with the following code but no luck

 $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;

请帮助..

推荐答案

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

请注意,当您使用 json_decode() 已经返回一个数组时,用true作为第二个参数调用它.

Note that json_decode() already returns an array when you call it with true as second parameter.

更新:

作为JSON中feed的值

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

是一个对象数组,$array['feed']的内容是:

is an array of objects, the content of $array['feed'] is:

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

要获取URL,您必须使用$array['feed'][0]['href']$feed[0]['href']访问数组.

To get the URL you have to access the array with $array['feed'][0]['href'] or $feed[0]['href'].

但这是数组的基本处理.也许阵列文档可以帮助您.

But this is basic handling of arrays. Maybe the Arrays documentation helps you.

这篇关于如何解码此JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆