JQuery的:合并多个JSON结果多个、结果、JQuery、JSON

2023-09-10 13:48:05 作者:壹缕繁华如似梦

情况:用户想要导入的Youtube播放列表中使用YouTube的JSON API jQuery的网站

Situation: User want to import Youtube playlist in a JQuery site using Youtube's JSON API.

问题:的Youtube只给回前50个条目,但播放列表可超过100项长(长度由JSON响应totalItems'给出)。所有条目需要合并为1对象,需要在端被推入输出功能

Problem: Youtube only gives back first 50 entries, but playlists can be 100+ entries long (length is given by 'totalItems' in JSON response). All the entries need to be merged as 1 object and need to be pushed into an output function at the end.

输入1-50:http://gdata.youtube.com/feeds/api/playlists/84780DAC99E1A285?v=2&alt=jsonc&max-results=50&start-index=1

输入50-100:http://gdata.youtube.com/feeds/api/playlists/84780DAC99E1A285?v=2&alt=jsonc&max-results=50&start-index=50

当前code:

function fetchPlaylist(pid) {
    var the_url = 'http://gdata.youtube.com/feeds/api/playlists/' + encodeURIComponent(pid) + '?v=2&alt=jsonc&max-results=50';
    $.ajax({
        type: "GET",
        url: the_url,
        dataType: "jsonp",
        success: function (responseData, textStatus, XMLHttpRequest) {
            if (responseData.data != null) {
                if (responseData.data.items) {
                    outputFunction(responseData.data.items);
                } else {
                    console.log('No results for playlist: "' + pid + '"');
                }
            }
        }
    });
}

问:

什么是重复的JSON调用,直到所有的项目都获得了最有效的方式?

What is the most efficient way to repeat the JSON calls until all the entries are received?

如何的不同反应组合成一个对象吗?

How to combine the different responses into one object?

我已经试过:

与使用 http://api.jquery.com/jQuery.extend/,卡住了,因为复杂的/嵌套的情况

Merging with the use of http://api.jquery.com/jQuery.extend/ , got stuck because of the complex/nested situation

推荐答案

既然你只是想这是一个数组中的项目,你可以使用的 $合并() 在重复功能:

Since you just want the items which is an array, you can use $.merge() in a repeating function:

function fetchPlaylist(pid, start, items) {
  items = items || [];
  start = start || 0;
  var the_url = 'http://gdata.youtube.com/feeds/api/playlists/' + encodeURIComponent(pid) + '?v=2&alt=jsonc&max-results=50';
  $.ajax({
      type: "GET",
      url: the_url,
      data: { start: start },
      dataType: "jsonp",
      success: function(responseData, textStatus, XMLHttpRequest) {
        if (responseData.data != null) {
          if (responseData.data.items) {
            $.merge(items, responseData.data.items); //add to items
            if (responseData.data.totalItems > start + 50) {
                fetchPlaylist(pid, start + 50, items);
            } else {
                outputFunction(items);
            }
          } else {
            console.log('No results for playlist: "' + pid + '"');
          }
        }
      }
  });
}

你可以试试看这里,你只需要调用它的播放列表ID,像这样的:

You can give it a try here, you just call it with the playlist id, like this:

fetchPlaylist("84780DAC99E1A285");​

每当我们看到了请求完成,如果 totalItems 即YouTube的返回比我们要求更高的加50,如果是这样的话,再次读取,添加这些结果。 ..如果没有,那么我们就大功告成了,合并的数组传递到 outputFunction()

Each time the request completes we see if the totalItems that youtube returns is higher than what we requested plus 50, if that's the case, fetch again, add those results...if not then we're done and pass the combined array to the outputFunction().