使用jQuery检索并显示包含hashtag的tweet

时间:2011-03-16 11:49:16

标签: jquery json twitter hashtag

通过$ _GET方法,我从前一页的下拉列表中收到两个#标签变量(橄榄球队)。 然后将这些存储在我试图操作的JavaScript变量中,以显示有关团队1的最新推文。

我似乎正在努力(因为没有显示任何内容)与twitter搜索网址的构造或检索推文的函数本身($ .getJSON(url,function(tweet))。无论如何,代码是下面。谢谢你的期待;

 <div id="mainContent">
 <script type="text/javascript"> 
 var team1, team2; 
 team1 = '<?php echo $_GET["team1"]; ?>'; 
 team2 = '<?php echo $_GET["team2"]; ?>'; 
 </script>

<div id="last-tweet">
</div>

<script type="text/javascript">
$(document).ready(function(){
var format='json'; // set format
var url='http://search.twitter.com/search.json?q=%23' + team1 + '&callback=?';
$.getJSON(url,function(results){ // get the tweets
$("#last-tweet").html(results[0].text); // get the first tweet in the response and place it inside the div
});
});

更新: 我已经检查了返回的json文件,似乎我用来引用数据的关键字应该是'results'而不是'tweet'(例如,results [0])。 但是我已经做了这个调整,但仍然没有运气。 Firebug报告的结果未定义'。

json文件的片段如下:

{"results":[{"from_user_id_str":"118766757","profile_image_url":"http://a0.twimg.com/profile_images/1128884063/ManchesterUnited07_normal.jpg","created_at":"Thu, 17 Mar 2011 11:02:21 +0000","from_user":"sarthak_dev90","id_str":"48338376365056000","metadata":{"result_type":"recent"},"to_user_id":null,"text":"#ificouldiwouldbringback Tevez to #MUFC. He made a difference almost every time he played.", 

1 个答案:

答案 0 :(得分:0)

只需从浏览器http://search.twitter.com/search.json?q=liverpool访问以下网址,然后将输出json保存在文件中,然后分析json的格式..

例如,如果json输出是

{"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

你的'tweet [0] .text'应转换为

bindings[0].method    // will output "newURI"