Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

php - how to decode this JSON string?

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?qu003dhttp://itcapsule.blogspot.com/u0026outputu003djson",
  "title": "Feed results for "http://itcapsule.blogspot.com/"",
  "self": [{
    "href": "http://www.google.com/reader/api/0/feed-finder?qu003dhttp://itcapsule.blogspot.com/u0026outputu003djson"
  }],
  "feed": [{
    "href": "http://itcapsule.blogspot.com/feeds/posts/default"
  }]
}

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;

Please help ..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$array = json_decode($json, true);
$feed = $array['feed'];

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

Update:

As the value of feed in JSON

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...