您好,从Twitter返回的JSON访问元素时遇到了一些问题。我试图从6个不同的帐户中检索大约18条推文并按时间顺序显示它们,但我无法弄清楚如何访问数组中的元素。我尝试使用json_decode()
,尝试foreach
循环和for循环。基本上,我满足以下所有要求,我需要的是以下元素:text,user-> screen_name,user-> profile_image_url和created_at。原因是当我将php上传到网站时,我正在构建的应用程序超时并且没有收到数据,因此我试图通过仅返回所需的内容来使返回更小。 Anywho,这是我收到的1条推文的样本......
[{"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 :(得分: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(在某些值中删除了单引号 - 只需转义它们或在解码/引用时转换它们)
答案 1 :(得分:0)
使用http://json.parser.online.fr/验证您尝试{J}的JSON是否有效。这是你需要使用的主要功能,它将返回一个你可以正常使用的数组。很可能你的JSON无效,你需要先清理它。
答案 2 :(得分:0)
没有结束括号,因此它不是有效的JSON数据。将]
放在字符串的末尾,然后使用json_decode()
;