$yt_id = "RWtxqtBTkpY";
$yt_data = file_get_contents('http://gdata.youtube.com/feeds/api/videos/' . $yt_id . '?v=2&alt=jsonc');
$decoded_josn = json_decode($yt_data);
//var_dump($decoded_josn->data);
foreach($decoded_josn->data as $key => $value)
print $key . ': ' . $value . '<br>';
这是我得到的,但var_dump();
id: RWtxqtBTkpY
uploaded: 2012-05-29T18:57:42.000Z
updated: 2014-06-30T12:50:11.000Z
uploader: babastarschannel
category: Entertainment
title: BABASTARS - HIGH (Official Video 2012)
description: Rrethi jon i gjon
Catchable fatal error: Object of class stdClass could not be converted to string in C:\Apache24\htdocs\yt_title.php on line 23
的var_dump($ decoded_josn-&GT;数据);
object(stdClass)#2 (18) { ["id"]=> string(11) "RWtxqtBTkpY" ["uploaded"]=> string(24) "2012-05-29T18:57:42.000Z" ["updated"]=> string(24) "2014-06-30T12:50:11.000Z" ["uploader"]=> string(16) "babastarschannel" ["category"]=> string(13) "Entertainment" ["title"]=> string(38) "BABASTARS - HIGH (Official Video 2012)" ["description"]=> string(17) "Rrethi jon i gjon" ["thumbnail"]=> object(stdClass)#3 (2) { ["sqDefault"]=> string(46) "http://i1.ytimg.com/vi/RWtxqtBTkpY/default.jpg" ["hqDefault"]=> string(48) "http://i1.ytimg.com/vi/RWtxqtBTkpY/hqdefault.jpg" } ["player"]=> object(stdClass)#4 (2) { ["default"]=> string(71) "http://www.youtube.com/watch?v=RWtxqtBTkpY&feature=youtube_gdata_player" ["mobile"]=> string(42) "http://m.youtube.com/details?v=RWtxqtBTkpY" } ["content"]=> object(stdClass)#5 (3) { ["5"]=> string(73) "http://www.youtube.com/v/RWtxqtBTkpY?version=3&f=videos&app=youtube_gdata" ["1"]=> string(102) "rtsp://r6---sn-4g57kue6.c.youtube.com/CiILENy73wIaGQmWklPQqnFrRRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" ["6"]=> string(102) "rtsp://r6---sn-4g57kue6.c.youtube.com/CiILENy73wIaGQmWklPQqnFrRRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" } ["duration"]=> int(221) ["rating"]=> float(4.2605257) ["likeCount"]=> string(4) "5150" ["ratingCount"]=> int(6318) ["viewCount"]=> int(1730090) ["favoriteCount"]=> int(0) ["commentCount"]=> int(1571) ["accessControl"]=> object(stdClass)#6 (8) { ["comment"]=> string(7) "allowed" ["commentVote"]=> string(7) "allowed" ["videoRespond"]=> string(9) "moderated" ["rate"]=> string(7) "allowed" ["embed"]=> string(7) "allowed" ["list"]=> string(7) "allowed" ["autoPlay"]=> string(7) "allowed" ["syndicate"]=> string(7) "allowed" } }
答案 0 :(得分:1)
您的JSON具有嵌套对象,例如(伪代码)
$obj = object (
'obj2' => object (
'obj3' => object ( ... )
)
)
当你在顶级$obj
上循环时,你最终会做相同的
echo $obj['obj2']
并尝试直接回显出一个对象。由于它们是stdClass
,因此它们没有魔法__toString()
方法,并且无法转换为字符串,这会导致您的错误。