小编典典

如何解码此JSON字符串?

php

这是我从提要查找器URL(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"
  }]
}

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

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

请帮忙 ..


阅读 402

收藏
2020-05-29

共1个答案

小编典典

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

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

更新:

作为feedJSON中的值

"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']

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

2020-05-29