Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取JSON对象中的项目,并根据发布日期对其进行排序_Javascript_Xml_Json_Windows_Rss - Fatal编程技术网

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

Javascript 获取JSON对象中的项目,并根据发布日期对其进行排序,javascript,xml,json,windows,rss,Javascript,Xml,Json,Windows,Rss,我正在使用javaScript使用Windows8应用程序。我得到一些rss提要,并使用GoogleAPI将其转换为JSON文件。所以文件内容是这样的 {"responseData":{"feed":{"feedUrl":"http://dmadmin.dailymirror.lk/index.php?option=com_ninjarsssyndicator&feed_id=17&format=raw","title":"Business","link":"http://dma

我正在使用javaScript使用Windows8应用程序。我得到一些rss提要,并使用GoogleAPI将其转换为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":[]},...
{“responseData”:{“feed”:{“feedUrl”:http://dmadmin.dailymirror.lk/index.php?option=com_ninjarsssyndicator&feed_id=17&format=raw“,”标题“:”业务“,”链接“:”http://dmadmin.dailymirror.lk/“,”作者“:”描述“:”类型“:”rss20“,”条目“:[{”标题“:”埃克森,壳牌可能在斯里兰卡石油、天然气区块拍卖中出价:萨利亚“,”链接“:"http://dmadmin.dailymirror.lk/business/economy/36146-exxon-shell-may-bid-in-sri-lanka-oil-gas-block-auction-saliya-.html“,”作者“:”出版日期“:”2013年9月26日星期四21:50:19-0700“,”内容片段“:”石油巨头埃克森美孚公司、荷兰皇家壳牌有限公司和法国道达尔公司表示有兴趣投标所提供的区块……,”内容“:”该岛国上游监管机构昨日表示,石油巨头埃克森美孚公司、荷兰皇家壳牌公司和法国道达尔公司对竞购斯里兰卡当前一轮许可证交易中提供的区块表现出了兴趣。
,“类别”:[]},{“标题”:“2013年巴基斯坦世博会十大斯里兰卡企业”,“链接”:http://dmadmin.dailymirror.lk/business/other/36144-ten-prominent-sri-lankan-businesses-at-pakistan-expo-2013-.html“,”作者“:”出版日期“:”2013年9月26日星期四21:45:47-0700“,”内容片段“:”巴基斯坦驻斯里兰卡高级专员卡西姆·库雷希少将主持了斯里兰卡商人参与巴基斯坦的活动……”,“内容”:”巴基斯坦驻斯里兰卡高级专员卡西姆·库雷希少将昨天在巴基斯坦高级专员公署与出口发展委员会官员一起,在前往卡拉奇之前,主持了2013年巴基斯坦博览会的斯里兰卡商人。
“,“类别”:[]},。。。
和上面一样,我有几个JSON格式的rss提要。 1.我如何读取上述JSON文件中的每一项。。? 2.我需要将所有rss作为一个JSON,并根据每个项目的发布日期对其进行排序。我如何才能做到这一点


如果您能提供一些答案或建议..或示例文件,那就太好了。

要将字符串转换为JSON,请使用。然后您可以拉出
responseData.feed.entries
以获得条目数组。使用该数组上的方法按日期对条目进行排序。
sort()
采用一个比较函数,比较数组中的两个项目,以确定哪一个项目应排在第一位。您可以在比较函数中使用该函数将条目的publishedDate转换为日期。如果第一个日期在第二个之前,则减去日期将返回<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); 
});

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.Y2be api按日期排序排序sort()时的秒数。

Hi要将rss转换为json,请查看这些选项。@nkron.非常感谢您的支持。.这对我帮助很大。再次感谢。。