我可以将许多rss源转换为单个JSON文件吗?

时间:2013-10-07 06:55:54

标签: javascript xml json rss winjs

我正在使用 Java脚本

处理Windows 8应用

我的RSS源很少,比如:

  1. http://dmadmin.dailymirror.lk/index.php?option=com_ninjarsssyndicator&feed_id=16&format=raw

  2. http://dmadmin.dailymirror.lk/index.php?option=com_ninjarsssyndicator&feed_id=17&format=raw

  3. 以下函数获取每个rss feed并转换为JSON对象。但我想要做的是将所有rss提供给一个JSON对象。 (有两个rss feed。所以在函数调用之后它给了我两个独立的JSON对象。但是我想要一个对象)

        for (x = 0; x < listOfFeed.length; x++) {
            //loop x start
            feedburnerUrl = listOfFeed[x].url,
                feedUrl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&output=json&num=999&q=" + encodeURIComponent(feedburnerUrl);
    
            WinJS.xhr({
                url: feedUrl,
                responseType: "rss/json"
            }).done(function complete(result) { //result = [object XMLHttpRequest]  for the requested URLs                                                                                   
                var jsonData = JSON.parse(result.response);  //jsonData = [object Object] create Object
                var entries = jsonData.responseData.feed.entries; //entries = [object object][object object][object object]......
    
                entries.forEach(function (entry) { // process the entries...                                
    
                   console.log('{"title" :"' + entry.title + '","Date":"' + entry.publishedDate + '"},');                          
                });
            });
        }  //loop x finish
    }
    
    1. listOfFeed = rss网址的数组。
    2. entries =完整JSON对象中的每个对象(一个URL中有25个项目)。
    3. jsonData =每个网址的JSON格式。所以我得到了两个。但我想为这两个网址设置一个JSON对象。
    4. 感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

您可以使用Array.concat()加入条目数组。您还可以跟踪未完成请求的数量,然后在条目达到0时处理这些条目。例如:

var allEntries = [];
var pendingRequestCount = listOfFeed.length;

var onRequestFinished = function() {
  pendingRequestCount--;

  if (pendingRequestCount === 0) {
    allEntries.forEach(function (entry) { // process the entries...                                
      console.log('{"title" :"' + entry.title + '","Date":"' + entry.publishedDate + '"},');                          
    });
  }
};

for (x = 0; x < listOfFeed.length; x++) {
  ... // Same as before
  }).done(function complete(result) {
    var jsonData = JSON.parse(result.response);  
    var entries = jsonData.responseData.feed.entries; 

    allEntries = allEntries.concat(entries);

    onRequestFinished();        
  });
}  //loop x finish

您还应该处理失败的请求并调用onRequestFinished函数。

相关问题