为什么我无法访问从 YouTube 返回的 JSON 数组的元素?

发布于 2024-12-02 17:15:51 字数 5896 浏览 0 评论 0原文

我正在解析从 YouTube 返回的 JSON 数组,同时我使用它返回的条目内容创建了一个新数组。然后,我将该数组放入 foreach 循环中,并尝试访问我需要的信息。当我这样做时 echo $d->title->type 输出 text (这是它的值),但是当我尝试获取视频的标题时 < code>$d->title->$t 不起作用。以下是我正在处理的输出。我需要从此数组中得到的两条信息是前面提到的视频标题和 $d->media$group->media$content->url 这是用于在 iPhone 上部署的 url。

[{"id":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054"},"published":{"$t":"2010-09-21T17:36:05.000Z"},"updated":{"$t":"2011-08-18T16:43:57.000Z"},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#video"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/categories.cat","term":"Music","label":"Music"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Back"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Pocket"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Memory"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Back Pocket Memory"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"guitar"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"vocals"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Awesome"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"drums"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"bass"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"amazing band"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"amazing guitar"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"fun"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"funny"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"cats"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"cute puppies"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Obama"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"star wars"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"gaga"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"sexy"}],"title":{"$t":"Back Pocket Memory : 'Bergerac' Official Music Video","type":"text"},"content":{"$t":"Our official music video for the single from our album, 'Strangers'. Made by our amazing friends at 18mm Productions. Check them out at: www.18mmproductions.com. youtube.com\/18mmproductions, and Twitter: @18mmproductions. Enjoy the video, we are proud to see all of your lovely faces in it!","type":"text"},"link":[{"rel":"alternate","type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=jPF1JA6n054&feature=youtube_gdata"},{"rel":"http:\/\/gdata.youtube.com\/schemas\/2007#video.responses","type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054\/responses"},{"rel":"http:\/\/gdata.youtube.com\/schemas\/2007#video.related","type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054\/related"},{"rel":"http:\/\/gdata.youtube.com\/schemas\/2007#mobile","type":"text\/html","href":"http:\/\/m.youtube.com\/details?v=jPF1JA6n054"},{"rel":"self","type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054"}],"author":[{"name":{"$t":"BackPocketMemory"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/backpocketmemory"}}],"gd$comments":{"gd$feedLink":{"href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054\/comments","countHint":19}},"media$group":{"media$category":[{"$t":"Music","label":"Music","scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/categories.cat"}],"media$content":[{"url":"http:\/\/www.youtube.com\/v\/jPF1JA6n054?f=videos&app=youtube_gdata","type":"application\/x-shockwave-flash","medium":"video","isDefault":"true","expression":"full","duration":244,"yt$format":5},{"url":"rtsp:\/\/v8.cache3.c.youtube.com\/CiILENy73wIaGQme06cOJHXxjBMYDSANFEgGUgZ2aWRlb3MM\/0\/0\/0\/video.3gp","type":"video\/3gpp","medium":"video","expression":"full","duration":244,"yt$format":1},{"url":"rtsp:\/\/v7.cache5.c.youtube.com\/CiILENy73wIaGQme06cOJHXxjBMYESARFEgGUgZ2aWRlb3MM\/0\/0\/0\/video.3gp","type":"video\/3gpp","medium":"video","expression":"full","duration":244,"yt$format":6}],"media$description":{"$t":"Our official music video for the single from our album, 'Strangers'. Made by our amazing friends at 18mm Productions. Check them out at: www.18mmproductions.com. youtube.com\/18mmproductions, and Twitter: @18mmproductions. Enjoy the video, we are proud to see all of your lovely faces in it!","type":"plain"},"media$keywords":{"$t":"Back, Pocket, Memory, Back Pocket Memory, guitar, vocals, Awesome, drums, bass, amazing band, amazing guitar, fun, funny, cats, cute puppies, Obama, star wars, gaga, sexy"},"media$player":[{"url":"http:\/\/www.youtube.com\/watch?v=jPF1JA6n054&feature=youtube_gdata_player"}],"media$thumbnail":[{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/0.jpg","height":360,"width":480,"time":"00:02:02"},{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/1.jpg","height":90,"width":120,"time":"00:01:01"},{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/2.jpg","height":90,"width":120,"time":"00:02:02"},{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/3.jpg","height":90,"width":120,"time":"00:03:03"}],"media$title":{"$t":"Back Pocket Memory : 'Bergerac' Official Music Video","type":"plain"},"yt$duration":{"seconds":"244"}},"gd$rating":{"average":4.9322033,"max":5,"min":1,"numRaters":59,"rel":"http:\/\/schemas.google.com\/g\/2005#overall"},"yt$statistics":{"favoriteCount":"42","viewCount":"4813"}}]}

I'm parsing a JSON array returned from YouTube, and while doing so I created a new array with the contents of entry it returns. I then put that array into a foreach loop and try to access the info I need. When I do this echo $d->title->type outputs text (which is it's value), but when I try to get the title of the video $d->title->$t doesn't work. The following is the output I'm working with. The two pieces of info I need out of this array are the previously mentioned which is the title of the video and $d->media$group->media$content->url which is the url used to deploy on the iPhone.

[{"id":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054"},"published":{"$t":"2010-09-21T17:36:05.000Z"},"updated":{"$t":"2011-08-18T16:43:57.000Z"},"category":[{"scheme":"http:\/\/schemas.google.com\/g\/2005#kind","term":"http:\/\/gdata.youtube.com\/schemas\/2007#video"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/categories.cat","term":"Music","label":"Music"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Back"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Pocket"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Memory"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Back Pocket Memory"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"guitar"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"vocals"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Awesome"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"drums"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"bass"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"amazing band"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"amazing guitar"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"fun"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"funny"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"cats"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"cute puppies"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"Obama"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"star wars"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"gaga"},{"scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/keywords.cat","term":"sexy"}],"title":{"$t":"Back Pocket Memory : 'Bergerac' Official Music Video","type":"text"},"content":{"$t":"Our official music video for the single from our album, 'Strangers'. Made by our amazing friends at 18mm Productions. Check them out at: www.18mmproductions.com. youtube.com\/18mmproductions, and Twitter: @18mmproductions. Enjoy the video, we are proud to see all of your lovely faces in it!","type":"text"},"link":[{"rel":"alternate","type":"text\/html","href":"http:\/\/www.youtube.com\/watch?v=jPF1JA6n054&feature=youtube_gdata"},{"rel":"http:\/\/gdata.youtube.com\/schemas\/2007#video.responses","type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054\/responses"},{"rel":"http:\/\/gdata.youtube.com\/schemas\/2007#video.related","type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054\/related"},{"rel":"http:\/\/gdata.youtube.com\/schemas\/2007#mobile","type":"text\/html","href":"http:\/\/m.youtube.com\/details?v=jPF1JA6n054"},{"rel":"self","type":"application\/atom+xml","href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054"}],"author":[{"name":{"$t":"BackPocketMemory"},"uri":{"$t":"http:\/\/gdata.youtube.com\/feeds\/api\/users\/backpocketmemory"}}],"gd$comments":{"gd$feedLink":{"href":"http:\/\/gdata.youtube.com\/feeds\/api\/videos\/jPF1JA6n054\/comments","countHint":19}},"media$group":{"media$category":[{"$t":"Music","label":"Music","scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/categories.cat"}],"media$content":[{"url":"http:\/\/www.youtube.com\/v\/jPF1JA6n054?f=videos&app=youtube_gdata","type":"application\/x-shockwave-flash","medium":"video","isDefault":"true","expression":"full","duration":244,"yt$format":5},{"url":"rtsp:\/\/v8.cache3.c.youtube.com\/CiILENy73wIaGQme06cOJHXxjBMYDSANFEgGUgZ2aWRlb3MM\/0\/0\/0\/video.3gp","type":"video\/3gpp","medium":"video","expression":"full","duration":244,"yt$format":1},{"url":"rtsp:\/\/v7.cache5.c.youtube.com\/CiILENy73wIaGQme06cOJHXxjBMYESARFEgGUgZ2aWRlb3MM\/0\/0\/0\/video.3gp","type":"video\/3gpp","medium":"video","expression":"full","duration":244,"yt$format":6}],"media$description":{"$t":"Our official music video for the single from our album, 'Strangers'. Made by our amazing friends at 18mm Productions. Check them out at: www.18mmproductions.com. youtube.com\/18mmproductions, and Twitter: @18mmproductions. Enjoy the video, we are proud to see all of your lovely faces in it!","type":"plain"},"media$keywords":{"$t":"Back, Pocket, Memory, Back Pocket Memory, guitar, vocals, Awesome, drums, bass, amazing band, amazing guitar, fun, funny, cats, cute puppies, Obama, star wars, gaga, sexy"},"media$player":[{"url":"http:\/\/www.youtube.com\/watch?v=jPF1JA6n054&feature=youtube_gdata_player"}],"media$thumbnail":[{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/0.jpg","height":360,"width":480,"time":"00:02:02"},{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/1.jpg","height":90,"width":120,"time":"00:01:01"},{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/2.jpg","height":90,"width":120,"time":"00:02:02"},{"url":"http:\/\/i.ytimg.com\/vi\/jPF1JA6n054\/3.jpg","height":90,"width":120,"time":"00:03:03"}],"media$title":{"$t":"Back Pocket Memory : 'Bergerac' Official Music Video","type":"plain"},"yt$duration":{"seconds":"244"}},"gd$rating":{"average":4.9322033,"max":5,"min":1,"numRaters":59,"rel":"http:\/\/schemas.google.com\/g\/2005#overall"},"yt$statistics":{"favoriteCount":"42","viewCount":"4813"}}]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-12-09 17:15:51

$ 不是 PHP 变量名称中的有效字符。如果您的 JSON 解析器返回关联数组,请尝试以下操作:

$d['title']['$t']

如果解析器返回对象,那么这将不起作用。在这种情况下,请尝试以下解决方法:

$d->title->{'$t'}

只要 JSON 键包含不是有效 PHP 变量名称的字符(包括所有包含 $ 的名称),您就必须使用此语法。

$ is not a valid character in a PHP variable name. If your JSON parser returns associative arrays, try this:

$d['title']['$t']

If the parser returns objects then that won't work. In that case, try this workaround:

$d->title->{'$t'}

You will have to use this syntax whenever JSON keys contain characters that are not valid PHP variable names, which includes all names containing $.

相对绾红妆 2024-12-09 17:15:51

$d->title->$t 尝试使用变量变量(在本例中是变量属性)。要访问名称为“$t”的属性,您需要使其明确:

$d->title->{'$t'}

另一种方法是将 JSON 解码为数组,这使得访问语法更容易:

$d = json_decode($string, true);
echo $d['title']['$t'];

$d->title->$t tries to use variable variables (well, variable properties in this case). To access a property with the name "$t", you need to make that unambiguous:

$d->title->{'$t'}

An alternative is to decode the JSON into an array, which makes the access syntax easier:

$d = json_decode($string, true);
echo $d['title']['$t'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文