0

I was working on yahoo news feed data and i want my output in JSON format. here i am using "rss news feed api" to get the yahoo news data. So by default the output of RSS feed will be in XML format, but i want the output in JSON format.

here is the code which I am working on.

import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
//import java.net.HttpURLConnection;

public class JavaHttpsExample {
public static void main(String[] args) throws Exception {

    String httpsURL = "http://news.yahoo.com/rss/us";

    URL myurl = new URL(null, httpsURL, new sun.net.www.protocol.https.Handler());
    HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
    InputStream ins = con.getInputStream();
    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader in = new BufferedReader(isr);

    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
    }

    in.close();
} } 

I have also added format=json in the url to get the output as JSON. But I didn't get succeed by doing that.

http://news.yahoo.com/rss/us?format=json

I have tried many ways which in turn caused failure. Can anyone help me out for getting the JSON output.

bunny sunny
  • 301
  • 6
  • 15

2 Answers2

0

I think you want output=json instead of format=json Got that from here https://developer.yahoo.com/javascript/json.html#output

!Correction! Actually RSS IS xml so I don't think you can directly. You may be able to do what your trying with an actual yahoo api call instead of using the RSS feed. I did find something to do a conversion but I cant speak to its quality http://ejohn.org/projects/rss2json/

Paul Swetz
  • 2,234
  • 1
  • 11
  • 28
0

I got the JSON output by converting the obtained XML output into JSON. Here is the procedure that I followed

    String inputLine = null;
        try {
            while ((inputLine = in.readLine()) != null)    {                        
            newline+=inputLine; 
                    } 

    } catch(Exception e){}
    finally{System.out.println("*********XML_OUTPUT********"+newline);}

    try {
         String newline = "";JSONObject xmlJSONObj = XML.toJSONObject(newline);
         String Json = xmlJSONObj.toString();
         System.out.println("JSON OUTPUT  : "+Json);
   } catch (JSONException je) {
        System.out.println(je.toString());
    }

in.close();
bunny sunny
  • 301
  • 6
  • 15