2011-09-02 72 views
0

我正在解析從YouTube返回的JSON數組,並在創建一個新數組時使用它返回的條目內容。然後我把這個數組放到foreach循環中,並嘗試訪問我需要的信息。當我這樣做echo $d->title->type輸出text(這是它的價值),但是當我試圖獲得視頻$d->title->$t的標題不起作用。以下是我正在使用的輸出。我需要從這個數組中獲取的兩條信息是前面提到的,這是視頻的標題,$d->media$group->media$content->url是用於在iPhone上部署的網址。爲什麼無法訪問我從YouTube返回的JSON數組元素?

[{"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

可能的[使用美元符號作爲節點名稱訪問PHP對象]的副本(http://stackoverflow.com/questions/4378807/access-a-php-object-with-dollar-sign-as-node-name) – mario

回答

3

$不是PHP變量名中的有效字符。如果你的JSON解析器返回關聯數組,試試這個:

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

如果解析器返回一個對象,然後將無法工作。在這種情況下,嘗試以下解決方法:

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

你將不得不使用這個語法只要JSON的鍵包含無效PHP變量名,其中包括含$所有名稱字符。

+0

我嘗試過使用{$ t},但那也不起作用。你可以在上面的問題的評論部分看到代碼。 –

+2

那是因爲你沒有引用它。使用'{'$ t'}',而不是'{$ t}'。他們是不同的。 – cdhowie

+0

感謝您的快速反應,熱愛這個網站的支持,你們是最好的。 –

3

$d->title->$t嘗試使用變量變量(以及在這種情況下的變量屬性)。要訪問屬性名稱爲「$ T」,你需要的是明確的:

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

另一種方法是將JSON解碼成一個陣列,這使得訪問語法簡單:

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

$ data = json_decode($ json_formatted); (數據爲$ d) { $ info = $ d-> feed-> entry; ($ info as $ i) { echo $ i-> title - > {$ t}; echo $ i-> media $ group-> media $ content-> type。「
」; } –

+0

發佈了一些代碼,以便您可以看到我的PHP頁面的外觀。我嘗試解碼JSON,並嘗試{$ t} –

+2

@CA:'$ i-> title {$ t}'中的'$ t'需要用引號括起來:'$ i-> title {' $ T'}'。如果你省略引號,它將不起作用;它將嘗試訪問名稱包含在變量「$ t」中的成員。 – cdhowie