获取JSON对象中的项目并根据发布日期对其进行排序

时间:2013-10-05 03:29:53

标签: javascript xml json windows rss

我正在使用javaScript处理Windows 8应用程序。我得到一些rss feed并使用谷歌API我将其转换为JSON文件。所以文件内容就像这样,

{"responseData":{"feed":{"feedUrl":"http://dmadmin.dailymirror.lk/index.php?option=com_ninjarsssyndicator&feed_id=17&format=raw","title":"Business","link":"http://dmadmin.dailymirror.lk/","author":"","description":"","type":"rss20","entries":[{"title":"Exxon, Shell may bid in Sri Lanka oil, gas block auction: Saliya","link":"http://dmadmin.dailymirror.lk/business/economy/36146-exxon-shell-may-bid-in-sri-lanka-oil-gas-block-auction-saliya-.html","author":"","publishedDate":"Thu, 26 Sep 2013 21:50:19 -0700","contentSnippet":"Oil majors Exxon Mobil Corp, Royal Dutch Shell PLC and France&rsquo;s Total have shown interest in bidding for blocks offered ...","content":"<img alt=\"\" src=\"http://cdn1.dailymirror.lk/media/images/oil(4).jpg\" style=\"width:90px;height:60px;margin:2px 5px;float:left\">Oil majors Exxon Mobil Corp, Royal Dutch Shell PLC and France’s Total have shown interest in bidding for blocks offered in Sri Lanka’s current licencing round, the island nation’s upstream regulator said yesterday.<br>","categories":[]},{"title":"Ten prominent Sri Lankan businesses at Pakistan Expo 2013","link":"http://dmadmin.dailymirror.lk/business/other/36144-ten-prominent-sri-lankan-businesses-at-pakistan-expo-2013-.html","author":"","publishedDate":"Thu, 26 Sep 2013 21:45:47 -0700","contentSnippet":"Pakistan High Commissioner in Sri Lanka Major General Qasim Qureshi hosted the Sri Lankan businessmen participating in Pakistan ...","content":"<img alt=\"\" src=\"http://cdn1.dailymirror.lk/media/images/pak(2).jpg\" style=\"width:90px;height:60px;margin:2px 5px;float:left\">Pakistan High Commissioner in Sri Lanka Major General Qasim Qureshi hosted the Sri Lankan businessmen participating in Pakistan Expo 2013 along with the officials of the Export Development Board yesterday, at the Pakistan High Commission, prior to their departure for Karachi.<br>","categories":[]},...

与上面相同我有几个JSON格式的RSS源。  1.我如何阅读上述JSON文件中的每个项目..?  2.我需要将所有RSS作为一个JSON,并根据每个项目的发布日期对其进行排序。我怎么能这样做?

如果您能提供一些答案或建议或示例文件,那将非常友好?

2 个答案:

答案 0 :(得分:0)

要将字符串转换为JSON,请使用JSON.parse(string)。然后,您可以提取responseData.feed.entries以获取条目数组。使用该数组上的sort()方法按日期对条目进行排序。 sort()采用比较函数,比较数组中的两个项,以查看哪个项应该首先出现。您可以在比较函数中使用Date.parse()将条目的publishedDate转换为Date。减去日期将返回&lt;如果第一个在第二个之前,则为0;如果它们相等则为0,并且>如果第一个在第二个之后,则为1。

以下是一个例子:

var response = JSON.parse('{"responseData":...');
var entries = response.responseData.feed.entries;

entries.sort(function(entry1, entry2) {
  // Compare the entries by publish date
  return Date.parse(entry1.publishedDate) - Date.parse(entry2.publishedDate);
});

entries.forEach(function(entry) { 
  // process the entries...
  console.log(entry.title + ': ' + entry.publishedDate); 
});

答案 1 :(得分:0)

Youtube Api的示例:

$.get( 'https://www.googleapis.com/youtube/v3/search?key=A[...]' , function( data ) {
    var itmn = [];
     $.each( data.items, function( k, val ) {
        var dt = val.snippet.publishedAt.replace(/\..+/g,"");
        dt = new Date( dt );
        itmn[k] = {};
        itmn[k]['date'] = dt.getTime();
        itmn[k]['id'] = k;
        itmn[k]['videoId'] = val.id.videoId; // Youtube
        itmn[k]['title'] = val.snippet.title; // Youtube
    });

    itmn.sort(function(e1, e2) {
        return parseInt( e1.date ) - parseInt( e2.date );
    });
    itmn.reverse();
    $.each( itmn, function( key, val ) {
        if ( val.videoId !== undefined ) {
            //Do somthing
        }
    });

});

PS。当sort()

时,按日期排序的Y2be api加标秒数