【问题标题】:how to decode this JSON string?如何解码这个 JSON 字符串?
【发布时间】:2011-01-15 04:13:35
【问题描述】:

这是我从提要查找器 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() 函数对其进行解码并获取最后一个数组元素(“feed”)?我用下面的代码试过了,但没有运气

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

请帮忙..

【问题讨论】:

  • 抱歉,如果我误解了,但您不想要 $ar['feed'] 吗?
  • PHP 中不需要类型转换。
  • @Jasonbar,是的,你是正确的。我只需要“feed”值。
  • (array) 前面的json_decode() 是不需要的。而且没有任何作用。

标签: php json


【解决方案1】:
$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 可以帮助你。

【讨论】:

  • @Felix,现在我得到的结果是“数组”:(
猜你喜欢
  • 1970-01-01
  • 2019-05-26
  • 2011-08-27
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多