2011-04-28 43 views
0

我試圖從Topsy(http://code.google.com/p/otterapi/)獲得分頁的json響應,並且遇到合併對象的問題。我想在瀏覽器中這樣做,因爲api速率限制是每個ip /用戶,並且低到可以做服務器端的事情。使用JavaScript合併JSON api響應

這是我的代碼。有沒有更好的辦法?當然,這是因爲這不起作用。我想我想讓這個工作,但也要了解是否有一個更安全,和/或更有效的方式。

該錯誤消息我得到的是...

類型錯誤:表達式「window.holdtweetslist.prototype」的結果[未定義]不是一個對象。

在此先感謝。

乾杯 斯蒂芬

$("#gettweets").live('click', function(event){ 

     event.preventDefault(); 
     getTweets('stephenbaugh'); 

    }); 



    function getTweets(name) { 

     var MAX_TWEETS = 500; 
     var TWEETSPERPAGE = 50; 
     var BASE = 'http://otter.topsy.com/search.json?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&[email protected]' + name + '&page=1'; 

     var currentpage = 1; 
      alert(BASE); 

     $.ajax({ 
      dataType: "json", 
      url: BASE, 
      success: function(data) { 

       window.responcesreceived = 1; 
       var response=data.response; 
       alert(response.total); 
       window.totalweets = response.total; 

       window.pagestoget = Math.ceil(window.totalweets/window.TWEETSPERPAGE); 

       window.holdtweetslist = response.list; 

       window.holdtweetslist.prototype.Merge = (function (ob) {var o = this;var i = 0;for (var z in ob) {if (ob.hasOwnProperty(z)) {o[z] = ob[z];}}return o;}); 

     // alert(data); 
      ;; gotTweets(data); 

       var loopcounter = 1; 
       do 
       { 
        currentpage = currentpage + 1; 
        pausecomp(1500); 
        var BASE = 'http://otter.topsy.com/search.json?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&[email protected]' + name + '&page=' + currentpage; 
alert(BASE); 
        $.ajax({dataType: "json", url: BASE, success: gotTweets(data)}); 
       } 
       while (currentpage<pagestoget); 

      } 
     }); 
    }; 

    function gotTweets(data) 
    { 
     window.responcesreceived = window.responcesreceived + 1; 
     var response = data.response; 
     alert(response.total); 
     window.holdtweetslist.Merge(response.list); 
     window.tweetsfound = window.tweetsfound + response.total; 
     if (window.responcesreceived == window.pagestoget) { 
      // sendforprocessingsendtweetlist(); 
      alert(window.tweetsfound); 
     } 
    } 

回答

0

謝謝埃德加和維普爾的幫助。不幸的是,他們能夠回答我的問題。我設法解決了這個問題是jquery的組合,不能正確解析json並需要使用jsonp和topsy。

這是我創建的一個小測試。

創建上顯示這個對象文檔....

<a href="#" id="gettweets">RUN TEST</a> 

您需要JQUERY

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 

並把下面的腳本了。通過Topsy所需的推文數量循環播放。

再次感謝大家。

$("#gettweets").live('click', function(event){ 

    event.preventDefault(); 
    getTweets('stephenbaugh'); 

}); 


    var MAX_TWEETS = 500; 
    var TWEETSPERPAGE = 50; 
    var BASE = 'http://otter.topsy.com/search.json'; 
    var currentpage; 
    var responcesreceived; 
    var totalweets; 
    var pagestoget; 
    var totalweets; 
    var TWEETSPERPAGE; 
    var holdtweetslist = []; 
    var requestssent; 
    var responcesreceived; 
    var tweetsfound; 
    var nametoget; 


function getTweets(name) { 

    nametoget=name; 
    currentpage = 1; 
    responcesreceived = 0; 
    pagestoget = 0; 

    var BASE = 'http://otter.topsy.com/search.js?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&[email protected]' + nametoget + '&page=1'; 
    $('#gettweets').html(BASE); 
    $.ajax({url: BASE, 
    dataType: 'jsonp', 
    success : function(data) { 
      getalltweets(data); 
     } 
    }); 
}; 


function getalltweets(data) { 

     totalweets = data.response.total; 
     $('#gettweets').append('<p>'+"total tweets " + totalweets+'</p>'); 
     $('#gettweets').append('<p>'+"max tweets " + MAX_TWEETS+'</p>'); 
     if (MAX_TWEETS < totalweets) { 
      totalweets = 500 
     } 
     $('#gettweets').append('<p>'+"new total tweets " + totalweets+'</p>'); 


     gotTweets(data); 



     pagestoget = Math.ceil(totalweets/TWEETSPERPAGE); 

     var getpagesint = self.setInterval(function() { 

      currentpage = ++currentpage; 

      var BASE = 'http://otter.topsy.com/search.js?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&[email protected]' + nametoget + '&page=' + currentpage; 
      $.ajax({url: BASE, 
       dataType: 'jsonp', 
       success : function(data) { 
        gotTweets(data); 
        } 
      }); 
      if (currentpage == pagestoget) { 
       $('#gettweets').append('<p>'+"finished sending " + currentpage+ ' of ' + pagestoget + '</p>'); 
       clearInterval(getpagesint); 
      }; 

     }, 2000); 
}; 


function gotTweets(data) 
{ 

     responcesreceived = responcesreceived + 1; 
     holdlist = data.response.list; 

     for (x in holdlist) 
     { 
      holdtweetslist.push(holdlist[x]); 
     } 

    // var family = parents.concat(children); 


     $('#gettweets').append('<p>receipt # ' + responcesreceived+' - is page : ' +data.response.page+ ' array length = ' + holdtweetslist.length +'</p>'); 
     //  holdtweetslist.Merge(response.list); 
     tweetsfound = tweetsfound + data.response.total; 
     if (responcesreceived == pagestoget) { 
     // sendforprocessingsendtweetlist(); 
      $('#gettweets').append('<p>'+"finished receiving " + responcesreceived + ' of ' + pagestoget + '</p>'); 
     } 

} 
1

要調用Merge作爲一個靜態方法,但它聲明爲「實例」的方法(對於prototype保留字)。

從合併聲明刪除prototype,所以你必須:

window.holdtweetslist.Merge = (function(ob)... 

這將解決JavaScript錯誤。

+0

謝謝埃德加。我現在得到一個不同的錯誤。我想我仍然相當困惑之間的數組,和JSON對象等... ReferenceError:無法找到變量:ARRAY – 2011-04-28 08:17:37

+0

不客氣。嗯..它是否直接說「ARRAY」? PS:如果你認爲它有用,請提高我的答案。謝謝 – 2011-04-28 08:22:21

+0

我已經提出了答案,但實際上並沒有解決它。是的,這是我現在得到的錯誤消息。歡呼 – 2011-04-28 08:27:15

1

這是Topsy的Vipul。你會分享你收到的文字JSON嗎?我想確保你沒有收到破壞的迴應。

+0

這就是我得到的,如果我在瀏覽器中運行它....對不起,我不能填充響應,因爲沒有足夠的字符。但你可以在這裏看到http://otter.topsy.com/search.json?type=tweet&perpage=5&window=a&nohidden=0&[email protected]&page=1 – 2011-04-29 01:41:58

+0

感謝您留意Vipul BTW的帖子。我認爲我的腳本不是api,而是錯誤的。很酷的服務,歡呼。謝謝 – 2011-04-29 01:43:29