开发者

Merging JSON api Response using Javascript

开发者 https://www.devze.com 2023-03-01 01:29 出处:网络
I am trying get paged json responses from Topsy (http://code.google.com/p/otterapi/) and am having problems merging the objects. I want to do this in browser as the api rate limit is per ip/user and t

I am trying get paged json responses from Topsy (http://code.google.com/p/otterapi/) and am having problems merging the objects. I want to do this in browser as the api rate limit is per ip/user and to low to do things server side.

Here is my code. Is there a better way? Of course there is because this doesn't work. I guess I want to get this working, but also to understand if there is a safer, and/or more efficient way.

The error message I get is ...

TypeError: Result of expression 'window.holdtweetslist.prototype' [undefined] is not an object.

Thanks in advance.

Cheers Stephen

    $("#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&q=@' + 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
             开发者_如何学Go   {
                    currentpage = currentpage + 1;
                    pausecomp(1500);
                    var BASE = 'http://otter.topsy.com/search.json?type=tweet&perpage=' + TWEETSPERPAGE + '&window=a&nohidden=0&q=@' + 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);
        }
    }


You are calling Merge as an static method, but declared it as an "instance" method (for the prototype reserved word).

Remove prototype from Merge declaration, so you'll have:

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

This will fix the javascript error.


This is Vipul from Topsy. Would you share the literal JSON you are receiving? I want to ensure you are not receiving a broken response.


THanks to Edgar and Vipul for there help. Unfortunately they were able to answer my questions. I have managed to work out that the issue was a combination of jquery not parsing the json properly and needing to use jsonp with topsy.

Here is a little test I created that works.

Create a doc with this object on it ....

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

You will need JQUERY

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

And put the following in a script too. The is cycle through the required number of tweets from Topsy.

Thanks again everyone.

$("#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&q=@' + 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&q=@' + 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>');
        }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消