【问题标题】:Merging JSON data from multiple URL's and sorting them based on a key合并来自多个 URL 的 JSON 数据并根据键对其进行排序
【发布时间】:2014-11-18 06:12:18
【问题描述】:

我需要一些关于 Javascript 的帮助。我有一些从 youtube API 收到的数据。数据是从以下 URL 检索的(我只显示了 2 个,但我也从多个其他渠道获得)

https://www.googleapis.com/youtube/v3/search?key=AIzaSyDuS9LWv86VFCFr4ZD_Kwp5--Zi6YKo_rM&part=snippet,id&order=date&maxResults=50&channelId=UCpVm7bg6pXKo1Pr6k5kxG9A

https://www.googleapis.com/youtube/v3/search?key=AIzaSyDuS9LWv86VFCFr4ZD_Kwp5--Zi6YKo_rM&part=snippet,id&order=date&maxResults=50&channelId=UCLQZTXj_AnL7fDC8sLrTGMw

这些 json 文件中的每个项目都有“publishedAt”值。现在我想合并两个 JSON 文件中的数据并根据“publishedAt”键对列表进行排序,即首先显示最新上传的视频。

这是我目前拥有的完美适用于一个文件的内容(我没有做任何魔术,URL 本身会根据日期对项目进行排序)

$.getJSON(sourceUrl, function (data) {
        //console.log(data);
        //var you_data = JSON.stringify(data);
        var videosCount = data.items.length;
        console.log("The number of videos is: " + videosCount);
        for ( i = 0 ;  i < videosCount; i++) {
            var title = data.items[i].snippet.title;
            var url = "https://www.youtube.com/watch?v=" + data.items[0].id.videoId;
            $("#reply").append("<a href=\"" + url + "\"> " + title + "</a><br><br><br>");
            //console.log(title);
            //console.log(url);
        };
});

我该如何完成这项工作?

已编辑(我的想法):

我能想到的是使用嵌套对象。我可以创建两个看起来像这样的新对象:

grand_parent_object = { {'publishedAt':xxxxxxxx, 'wholeItem':{JSON文件中显示的整个项目}}, {'publishedAt':xxxxxxxx, 'wholeItem':{JSON文件中显示的整个项目2文件}}等}

这里的 parent_object 是 {'publishedAt':xxxxxxxx, 'wholeItem':{the whole item as shown in the JSON file}}

也许我应该首先根据它们的 'publishedAt' 值对 parent_objects 进行排序,然后才能完成这项工作???? PS: parent_object 中的 'publishedAt' 与 'wholeItem' 值中的 'publishedAt' 相同。

解决方案:

我使用了罗斯的逻辑,它奏效了。我对 .getJson 有疑问,因为它不会更新全局变量 Wholearray。所以我使用了 .ajax 并且它起作用了。这是我的工作代码:

function getAjaxData(sourceUrl) {
$.ajax({
     async:false,
     url:sourceUrl,
     success: function(data) {
         var videosCount = data.items.length;
          for ( var i = 0 ;  i < videosCount; i++) {
              var tempobject = {};
              tempobject.published = data.items[i].snippet.publishedAt;
              tempobject.wholeItem = data.items[i];
              wholearray.push(tempobject);
          }
      }
     });
 }

【问题讨论】:

    标签: javascript json youtube


    【解决方案1】:

    一种解决方案是创建一个新的对象字面量数组,然后根据键对数组进行排序:

    var array = [];
    $.getJSON(url, function(data){
       for (var i=0; i<data.length; i++){
          var object = {}
          object.published = data.items[i].snippet.publishedAt
          object.wholeItem = data.items[i]
          array.push(object);
        }
     })
    $.getJSON(otherUrl, function(data){
       for (var i=0; i<data.length; i++){
          var object = {}
          object.published = data.items[i].snippet.publishedAt
          object.wholeItem = data.items[i]
          array.push(object);
        }
     })
    

    有一个监听器等待两个 AJAX 调用完成,然后你可以排序:

    array.sort(function(a,b) { return a.published - b.published; });
    

    This question gives more info on sorting

    这可能不是最有效的方法,但它是第一个想到的并且会很好用的方法!

    【讨论】:

    • 我也有类似的想法,但我使用了你的代码。但是我被困在“数组”没有被更新的地方。我将“array”声明为全局变量,当我尝试从“.getJSON”函数内部更新该变量时,它不起作用。除了这个 .getJSON 之外,从任何其他函数更新它都有效。任何其他全局变量都存在同样的问题。我无法在此评论中粘贴我的代码,将在下一条评论中粘贴。
    • var wholearray = []; &lt;&lt; global variable function getYouTubeUrls(sourceUrl) { $.getJSON(sourceUrl, function (data) { var videosCount = data.items.length; for ( var i = 0 ; i &lt; videosCount; i++) { var tempobject = {} tempobject.published = data.items[i].snippet.publishedAt; tempobject.wholeItem = data.items[i]; wholearray.push(tempobject); console.log(wholearray); }; });
    • 你能发一个 JSFiddle 吗?此外,您的密钥似乎不再适用于可能导致问题的 API。
    猜你喜欢
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多