我正在解析从YouTube返回的JSON数组,同时这样做我创建了一个新数组,其中包含返回的条目内容。然后我将该数组放入foreach
循环并尝试访问我需要的信息。当我执行此操作时echo $d->title->type
输出text
(这是它的值),但是当我尝试获取视频标题时$d->title->$t
不起作用。以下是我正在使用的输出。我需要从这个数组中获取的两条信息是前面提到的视频标题和$d->media$group->media$content->url
,它是用于在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"}}]}
答案 0 :(得分:3)
$
不是PHP变量名中的有效字符。如果您的JSON解析器返回关联数组,请尝试以下操作:
$d['title']['$t']
如果解析器返回对象,则无效。在这种情况下,请尝试以下解决方法:
$d->title->{'$t'}
只要JSON密钥包含的字符不是有效的PHP变量名,包含所有包含$
的名称,就必须使用此语法。
答案 1 :(得分:3)
$d->title->$t
尝试使用变量变量(在这种情况下,变量属性)。要访问名为“$ t”的属性,您需要明确说明:
$d->title->{'$t'}
另一种方法是将JSON解码为数组,这使得访问语法更容易:
$d = json_decode($string, true);
echo $d['title']['$t'];