0

I'm working with windows 8 app using javaScript. I get some rss feed and using google API I convert it to a JSON file. So the file content get like this,

{"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":[]},...

same as the above I have few rss feeds in JSON format. 1. How I read each item in the above JSON file..? 2. I need to get all the rss as one JSON and sort it according to the published date of each item. How can I do that?

It is very kind if you can provide some answers or suggestion..or sample files?

SilentCoder
  • 1,970
  • 1
  • 16
  • 21
  • Hi to convert rss to json take a look at these options.. http://stackoverflow.com/questions/670511/convert-rss-to-json – codebreaker Oct 05 '13 at 03:40

2 Answers2

0

To convert the string to JSON, use JSON.parse(string). Then you can pull out the responseData.feed.entries to get the Array of entries. Use the sort() method on that array to sort the entries by date. sort() takes a comparison function that compares two items in the array to see which one should come first. You can use Date.parse() in your comparison function to convert the entry's publishedDate to a Date. Subtracting the dates will return return < 0 if the first is before the second, 0 if they are equal, and > 1 if the first is after the second.

Here's an example:

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); 
});
nkron
  • 19,086
  • 3
  • 39
  • 27
0

Example for 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 sorting by date spiked seconds when sort()

user956584
  • 5,316
  • 3
  • 40
  • 50