Json代码是:
{"apiVersion":"2.1","data":{"id":"bLBSoC_2IY8","uploaded":"2014-04- 29T04:11:56.000Z","updated":"2014-12-04T16:08:10.000Z","uploader":"latenight","category":"Comedy","title":"Lip Sync Battle with Emma Stone","description":"Jimmy and Emma Stone square off in a lip sync battle and Emma shows she's not afraid of lightning fast lyrics.\n\nSubscribe NOW to The Tonight Show Starring Jimmy Fallon: http://bit.ly/1nwT1aN\n\nWatch The Tonight Show Starring Jimmy Fallon Weeknights 11:35/10:35c\nGet more Jimmy Fallon: \nFollow Jimmy: http://Twitter.com/JimmyFallon\nLike Jimmy: https://Facebook.com/JimmyFallon\n\nGet more The Tonight Show Starring Jimmy Fallon: \nFollow The Tonight Show: http://Twitter.com/FallonTonight\nLike The Tonight Show: https://Facebook.com/FallonTonight\nThe Tonight Show Tumblr: http://fallontonight.tumblr.com/\n\nGet more NBC: \nNBC YouTube: http://bit.ly/1dM1qBH\nLike NBC: http://Facebook.com/NBC\nFollow NBC: http://Twitter.com/NBC\nNBC Tumblr: http://nbctv.tumblr.com/\nNBC Google+: https://plus.google.com/+NBC/posts\n\nThe Tonight Show Starring Jimmy Fallon features hilarious highlights from the show including: comedy sketches, music parodies, celebrity interviews, ridiculous games, and, of course, Jimmy's Thank You Notes and hashtags! You'll also find behind the scenes videos and other great web exclusives.\n\nLip Sync Battle with Emma Stone\nhttp://www.youtube.com/fallontonight","thumbnail":{"sqDefault":"http://i.ytimg.com/vi/bLBSoC_2IY8/default.jpg","hqDefault":"http://i.ytimg.com/vi/bLBSoC_2IY8/hqdefault.jpg"},"player":{"default":"http://www.youtube.com/watch?v=bLBSoC_2IY8&feature=youtube_gdata_player","mobile":"http://m.youtube.com/details?v=bLBSoC_2IY8"},"content":{"5":"http://www.youtube.com/v/bLBSoC_2IY8?version=3&f=videos&d=AXyDziqd4CCanEZDalXV_C8O88HsQjpE1a8d1GxQnGDm&app=youtube_gdata","1":"rtsp://r7---sn-o097zuek.c.youtube.com/CkULENy73wIaPAmPIfYvoFKwbBMYDSANFEgGUgZ2aWRlb3NyIQF8g84qneAgmpxGQ2pV1fwvDvPB7EI6RNWvHdRsUJxg5gw=/0/0/0/video.3gp","6":"rtsp://r7---sn-o097zuek.c.youtube.com/CkULENy73wIaPAmPIfYvoFKwbBMYESARFEgGUgZ2aWRlb3NyIQF8g84qneAgmpxGQ2pV1fwvDvPB7EI6RNWvHdRsUJxg5gw=/0/0/0/video.3gp"},"duration":446,"aspectRatio":"widescreen","rating":4.9287434,"likeCount":"279315","ratingCount":284381,"viewCount":36983500,"favoriteCount":0,"commentCount":12125,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}}
我试图在php数组中获取数据,循环运行正常,但变量没有值:
foreach ($result->data as $item) {
$title = is_null($subject) ? htmlentities($item->title) : htmlentities($item->video->title);
$id = is_null($subject) ? $item->id : $item->video->id;
$timestamp = is_null($subject) ? strtotime($item->uploaded) : strtotime($item->video->uploaded);
$uploaded = date('M j, Y', $timestamp);
$watch = is_null($subject) ? $item->player->default : $item->video->player->default;
$image = is_null($subject) ? $item->thumbnail->sqDefault : $item->video->thumbnail->sqDefault;
$description = is_null($subject) ? $item->description : $item->video->description;
$viewCount = is_null($subject) ? $item->viewCount : $item->video->viewCount;
$duration = is_null($subject) ? gmdate('H:i:s', intval($item->duration)) : gmdate('H:i:s', intval($item->video->duration));
echo $id
echo $title
echo $description
?>
问题是什么?
答案 0 :(得分:1)
您正在使用错误的obj访问路径:
{"apiVersion":"2.1","data":{"id":"bLBSoC_2IY8","uploaded":"2014-04-etc..
foreach($obj->data as $item) {
将给你" blBSoC_2IY8"," 2014-04-etc. ..."作为$ item,它只是字符串。 $item->whatever
将失败,因为$ item是一个字符串,而不是一个对象。
答案 1 :(得分:1)
正如@Marc_B试图解释的那样,你需要遍历键/值对,如:
foreach($result->data as $key=>$value) {
寻找你感兴趣的$key
。
但是,如果数据始终包含一个结果,则可以跳过循环,只需:
echo $result->data->id;
echo $result->data->title;
echo $result->data->description;