2

ユーザーがランダムな単語を入力し、関連するツイートのリストを取得するWebサイトを開発しています。

リンク、返信、ハッシュタグを含むツイートを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>
4

1 に答える 1

0

Dev Twitter API リファレンスによると、返される JSON オブジェクトにはresult、すべてのツイートを表す JSON オブジェクトの配列である属性が含まれています。これらの JSON 配列で特に重要な 2 つの属性は、entitites属性とto_user_id属性です。したがって、ツイートが返信ではなく、リンクが含まれていないかどうかを確認するにentitiesは、 が空のオブジェクトで、to_user_idnull であるかどうかを確認します。

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);
    }
}​

(空の JavaScript オブジェクトをテストするにはどうすればよいですか? の回答を使用して、空のオブジェクトかどうかを確認していることに注意してくださいentitites。)

于 2012-06-06T20:13:35.943 に答える