使用 Javascript 合并 JSON api 响应

发布于 2024-11-03 15:46:57 字数 2473 浏览 1 评论 0原文

我正在尝试从 Topsy (http://code.google.com/p/otterapi/) 获取分页 json 响应,但在合并对象时遇到问题。我想在浏览器中执行此操作,因为 api 速率限制是针对每个 ip/用户的,并且在服务器端执行操作的速率较低。

这是我的代码。有更好的办法吗?当然有,因为这是行不通的。我想我想让这个工作正常进行,但也想了解是否有更安全和/或更有效的方法。

我收到的错误消息是...

TypeError: 表达式 'window.holdtweetslist.prototype' [undefined] 的结果不是对象。

提前致谢。

干杯 斯蒂芬

    $("#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
                {
                    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);
        }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2024-11-10 15:46:57

您将 Merge 作为静态方法调用,但将其声明为“实例”方法(对于 prototype 保留字)。

从 Merge 声明中删除 prototype,这样您将得到:

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

这将修复 javascript 错误。

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.

黯然#的苍凉 2024-11-10 15:46:57

我是 Topsy 的 Vipul。您能分享一下您收到的 JSON 文本吗?我想确保您不会收到不完整的回复。

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.

不知在何时 2024-11-10 15:46:57

感谢 Edgar 和 Vipul 的帮助。不幸的是他们能够回答我的问题。我已经设法解决这个问题是 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&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>');
        }

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文