2011-08-26 62 views
0

大家好,在從Twitter返回的JSON中訪問元素時都遇到一些問題。我試圖從6個不同的帳戶檢索大約18條推文,並按時間順序顯示它們,但我無法弄清楚如何訪問數組中的元素。我試過使用json_decode(),試過foreach循環和for循環。基本上我沒有做到以下全部,我需要的是以下元素:text,user-> screen_name,user-> profile_image_url和created_at。原因是當我將php上傳到網站時,我正在構建的應用程序超時並且沒有收到數據,所以我試圖通過只回收需要的東西來縮小回報。 Anywho,這裏是我收到回1個鳴叫的樣本...如何使用JSON中的元素在PHP中創建新數組返回

[{"in_reply_to_status_id":null,"created_at":"Thu Aug 25 01:50:07 +0000 2011","geo":null,"user":{"profile_background_tile":true,"protected":false,"url":"http:\/\/www.backpocketmemory.com","name":"Back Pocket Memory","default_profile":false,"friends_count":1115,"profile_link_color":"0084B4","default_profile_image":false,"show_all_inline_media":false,"contributors_enabled":false,"following":false,"geo_enabled":false,"utc_offset":-28800,"profile_sidebar_border_color":"C0DEED","description":"We're a hard rock\/pop band from Burbank, CA. We love you. facebook.com\/backpocketmemory @bpmchris @bpmguitar @bpmsixstring @bpmbass @bpmdrummer\r\n","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","time_zone":"Pacific Time (US & Canada)","created_at":"Mon Jul 27 20:58:35 +0000 2009","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","is_translator":false,"profile_use_background_image":true,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","follow_request_sent":false,"statuses_count":2238,"profile_background_color":"C0DEED","screen_name":"bckpcktmmry","profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","favourites_count":6,"id_str":"60708421","lang":"en","verified":false,"profile_text_color":"333333","location":"Burbank, CA","id":60708421,"listed_count":19,"notifications":false,"profile_sidebar_fill_color":"DDEEF6","followers_count":683},"in_reply_to_user_id":null,"truncated":false,"coordinates":null,"favorited":false,"possibly_sensitive":false,"in_reply_to_status_id_str":null,"in_reply_to_screen_name":null,"source":"\u003Ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003EFacebook\u003C\/a\u003E","in_reply_to_user_id_str":null,"id_str":"106543849266098176","id":106543849266098176,"contributors":null,"place":null,"retweeted":false,"retweet_count":2,"text":"Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http:\/\/t.co\/oNvw5I6"}]

任何幫助表示讚賞!

+0

您可以在某處發佈更完整的示例嗎? – sberry

+0

編輯OP在JSON結尾處添加「]」,但在粘貼時沒有出現。 @ sberry2A是什麼,我的PHP代碼或JSON返回更多的comlete示例? –

回答

1

你的JSON解碼罰款:

array (
    0 => 
    array (
    'in_reply_to_status_id' => NULL, 
    'created_at' => 'Thu Aug 25 01:50:07 +0000 2011', 
    'geo' => NULL, 
    'user' => 
    array (
     'profile_background_tile' => true, 
     'protected' => false, 
     'url' => 'http://www.backpocketmemory.com', 
     'name' => 'Back Pocket Memory', 
     'default_profile' => false, 
     'friends_count' => 1115, 
     'profile_link_color' => '0084B4', 
     'default_profile_image' => false, 
     'show_all_inline_media' => false, 
     'contributors_enabled' => false, 
     'following' => false, 
     'geo_enabled' => false, 
     'utc_offset' => -28800, 
     'profile_sidebar_border_color' => 'C0DEED', 
     'description' => 'We're a hard rock/pop band from Burbank, CA. We love you. facebook.com/backpocketmemory @bpmchris @bpmguitar @bpmsixstring @bpmbass @bpmdrummer 
', 
     'profile_background_image_url_https' => 'https://si0.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg', 
     'time_zone' => 'Pacific Time (US & Canada)', 
     'created_at' => 'Mon Jul 27 20:58:35 +0000 2009', 
     'profile_image_url' => 'http://a2.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg', 
     'is_translator' => false, 
     'profile_use_background_image' => true, 
     'profile_image_url_https' => 'https://si0.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg', 
     'follow_request_sent' => false, 
     'statuses_count' => 2238, 
     'profile_background_color' => 'C0DEED', 
     'screen_name' => 'bckpcktmmry', 
     'profile_background_image_url' => 'http://a3.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg', 
     'favourites_count' => 6, 
     'id_str' => '60708421', 
     'lang' => 'en', 
     'verified' => false, 
     'profile_text_color' => '333333', 
     'location' => 'Burbank, CA', 
     'id' => 60708421, 
     'listed_count' => 19, 
     'notifications' => false, 
     'profile_sidebar_fill_color' => 'DDEEF6', 
     'followers_count' => 683, 
    ), 
    'in_reply_to_user_id' => NULL, 
    'truncated' => false, 
    'coordinates' => NULL, 
    'favorited' => false, 
    'possibly_sensitive' => false, 
    'in_reply_to_status_id_str' => NULL, 
    'in_reply_to_screen_name' => NULL, 
    'source' => '<a href="http://www.facebook.com/twitter" rel="nofollow">Facebook</a>', 
    'in_reply_to_user_id_str' => NULL, 
    'id_str' => '106543849266098176', 
    'id' => 106543849266098176, 
    'contributors' => NULL, 
    'place' => NULL, 
    'retweeted' => false, 
    'retweet_count' => 2, 
    'text' => 'Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http://t.co/oNvw5I6', 
), 
) 

回聲所有用戶的頭像,名字,當他們加入了一個日期:

$data = json_decode($twitter_response); 
foreach($data as $d) 
{ 
    echo '<img src="' . $d->user->profile_image_url . '" /> ' . $d->user->screen_name . ' (since ' . $d->user->created_at . ')<br />'; 
} 

如果您使用的是關聯數組爲您json_decode輸出,請確保您參考相應的按鍵,如下所示:

foreach($data as $d) 
{ 
    echo '<img src="' . $d['user']['profile_image_url'] . '" /> ' . $d['user']['screen_name'] . ' (since ' . $d['user']['created_at'] . ')<br />'; 
} 

工作代碼:http://pastie.org/2436307(在某些值中刪除了單引號 - 只是在解碼/引用時將它們轉義或轉換)

+0

似乎解碼很好,我把我們的代碼放到了我的項目中,看它是否可以工作,但事實並非如此。甚至試過$ d-> created_at,但什麼也沒有。奇怪。感謝您的幫助,我要離開工作,並在我到達那裏時更仔細地檢查它。 –

+0

查看更新後的代碼,具體取決於json_decode標誌,您可能需要關聯數組方法。 – AlienWebguy

+0

它可以與$ d-> created_at一起使用。我急着開始工作,並沒有注意到我正在試圖用兩個$符號來解碼我的數組變量。拿出來,像魅力一樣工作,感謝您的幫助! –

0

使用http://json.parser.online.fr/來驗證您嘗試json_decode的JSON是否有效。這是你需要使用的主要功能,它會返回一個你可以正常使用的數組。很有可能你的JSON無效,你需要先清理掉。

+1

如果您花了兩秒鐘將JSON粘貼到JSONLint.com中,您會發現它是有效的:) – AlienWebguy

+0

該網站顯示JSON有效。 –

+0

感謝網站@Blair,在將來非常有用 –

0

沒有右括號,因此它不是有效的JSON數據。將]放在字符串的末尾,它解析得很好,用json_decode();

+0

謝謝,它實際上就在那裏,當我因任何原因粘貼時沒有標記,將它固定在OP中。 –

相關問題