2013-10-07 33 views
0

我使用窗口使用Java脚本我可以将很多rss提要转换为单个JSON文件吗?

我有几个RSS源,就像8应用:

  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

以下功能得到每个rss提供并转换为JSON对象但是我想要做的是获取所有rss feed到一个JSON对象。 (有两个RSS提要,因此函数调用后它给了我两个独立的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对象。

感谢您的帮助......

回答

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功能以及。

+0

非常感谢您的好意@nkron。一个问题。正如您之前所说的,我如何根据发布日期对所有条目进行排序?在哪里添加该方法..?我使用它添加到以前的地方。但它仅对每个Feed进行排序。 – SilentCoder

+0

实际上我可以对所有条目进行排序。然后我得到了我想要的结果。非常感谢@nkron。我是新来的这个领域,并希望你以后的帮助也..再次感谢你。 – SilentCoder

+0

@robi kumar,谢谢你编辑我的问题整齐地..非常感谢你 – SilentCoder

相关问题