使用JSON和jQuery获取推文时,跳过带有链接、回复或哈希标记的推文

使用JSON和jQuery获取推文时,跳过带有链接、回复或哈希标记的推文,jquery,json,twitter,Jquery,Json,Twitter,我正在开发一个网站,用户在其中输入一个随机单词,并获得相关tweet的列表 使用json获取包含链接、回复或哈希标记的推文时,如何排除这些推文 以下是我的jQuery代码: <script> function go(){ var url = "http://search.twitter.com/search.json?callback=results&q=" + $("#text").val(); $(

我正在开发一个网站,用户在其中输入一个随机单词,并获得相关tweet的列表

使用json获取包含链接、回复或哈希标记的推文时,如何排除这些推文

以下是我的jQuery代码:

        <script>

        function go(){
          var url = "http://search.twitter.com/search.json?callback=results&q=" + $("#text").val();
          $("<script/>").attr("src", url).appendTo("body");  
            $("#text").remove();
        }

        $("#text").keydown(function(e){ if( e.which == 13 )  go(); });

        function results(r){
          window.results = r.results;
          window.theIndex = 0;
          displayNext();
        }
        function displayNext(){
          if( window.theIndex >= window.results.length ){
            return;
          }
          $('.content').remove();
            $('.helper').remove();
          createDiv( window.results[window.theIndex] );
          window.theIndex++;
          setTimeout(displayNext, 4000);
        }

        function createDiv(status){
          var tweets = status.text;
          $("<span class='content'>")
          .html(tweets)
          .appendTo("body");
          $("<span class='helper'>")
          .appendTo("body")
        }

        </script>
根据,返回的JSON对象包含一个result属性,该属性是表示所有tweet的JSON对象数组。这些JSON数组中特别感兴趣的两个属性是entitites属性和to_user_id属性。因此,要检查tweet是否不是回复,并且不包含任何链接,您需要检查实体是否为空对象,to_user_id是否为空

将displayNext功能更改为此应该可以:

function displayNext(){
    if( window.theIndex >= window.results.length ){
        return;
    }
    $('.content').remove();
    $('.helper').remove();
    var result = window.results[window.theIndex];
    if (Object.keys(result.entities).length !== 0 && result.to_user_id === null) {
        createDiv( window.results[window.theIndex] );
        window.theIndex++;
        setTimeout(displayNext, 4000);
    }
}​
请注意,我使用的是来自的答案来检查entitites是否为空对象。

根据,返回的JSON对象包含一个result属性,它是一个JSON对象数组,表示所有tweet。这些JSON数组中特别感兴趣的两个属性是entitites属性和to_user_id属性。因此,要检查tweet是否不是回复,并且不包含任何链接,您需要检查实体是否为空对象,to_user_id是否为空

将displayNext功能更改为此应该可以:

function displayNext(){
    if( window.theIndex >= window.results.length ){
        return;
    }
    $('.content').remove();
    $('.helper').remove();
    var result = window.results[window.theIndex];
    if (Object.keys(result.entities).length !== 0 && result.to_user_id === null) {
        createDiv( window.results[window.theIndex] );
        window.theIndex++;
        setTimeout(displayNext, 4000);
    }
}​
注意,我使用的是来自的答案来检查entitites是否为空对象