【问题标题】:How to get JSON data for yahoo news feed api output如何获取雅虎新闻提要 api 输出的 JSON 数据
【发布时间】:2016-06-08 12:13:02
【问题描述】:

我正在处理 yahoo 新闻提要数据,我想要 JSON 格式的输出。这里我使用“rss news feed api”来获取雅虎新闻数据。所以默认情况下,RSS 提要的输出将是 XML 格式,但我想要 JSON 格式的输出。

这是我正在处理的代码。

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();
} } 

我还在 url 中添加了 format=json 以获取 JSON 格式的输出。但我这样做并没有成功。

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

我尝试了很多方法,最终导致失败。谁能帮我获取 JSON 输出。

【问题讨论】:

    标签: json rss


    【解决方案1】:

    我想你想要 output=json 而不是 format=json 从这里得到https://developer.yahoo.com/javascript/json.html#output

    !更正! 实际上 RSS 是 xml 所以我不认为你可以直接。您可以使用实际的 yahoo api 调用而不是使用 RSS 提要来完成您尝试的操作。 我确实找到了一些可以进行转换的东西,但我无法说出它的质量 http://ejohn.org/projects/rss2json/

    【讨论】:

    【解决方案2】:

    我通过将获得的 XML 输出转换为 JSON 来获得 JSON 输出。这是我遵循的程序

        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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多