0

我正在使用 javaScript 处理 Windows 8 应用程序。我得到一些 rss 提要并使用 google 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,并根据每个项目的发布日期对其进行排序。我怎样才能做到这一点?

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

4

2 回答 2

0

要将字符串转换为 JSON,请使用JSON.parse(string). 然后您可以拉出responseData.feed.entries以获取条目数组。使用该sort()数组上的方法按日期对条目进行排序。 sort()接受一个比较函数,比较数组中的两个项目,看看哪个应该先出现。您可以Date.parse()在比较函数中使用将条目的发布日期转换为日期。如果第一个在第二个之前,则减去日期将返回 < 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); 
});
于 2013-10-05T07:00:46.593 回答
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
        }
    });

});

附言。Y2be api 按日期排序时 sort()

于 2014-11-13T09:24:08.397 回答