简体   繁体   English

如何从Java Servlet检索JSON中的提要?

[英]How can I retrieve a feed in JSON from a Java Servlet?

I want to make an Http request and store the result in a JSONObject. 我想发出一个Http请求并将结果存储在JSONObject中。 I haven't worked much with servlets, so I am unsure as to whether I am 1) Making the request properly, and 2) supposed to create the JSONObject. 我在servlet方面工作不多,因此不确定我是否1)正确提出请求,以及2)应该创建JSONObject。 I have imported the JSONObject and JSONArray classes, but I don't know where I ought to use them. 我已经导入了JSONObject和JSONArray类,但是我不知道应该在哪里使用它们。 Here's what I have: 这是我所拥有的:

     public void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws IOException {

        //create URL    
        try {
            // With a single string.
            URL url = new URL(FEED_URL);

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
            }
            in.close();
        } catch (MalformedURLException e) {
        }
        catch (IOException e) {
        }

My FEED_URL is already written so that it will return a feed formatted for JSON. 我的FEED_URL已被写入,因此它将返回格式化为JSON的供稿。

This has been getting to me for hours. 这已经给我几个小时了。 Thank you very much, you guys are an invaluable resource! 非常感谢您,你们是宝贵的资源!

First gather the response into a String: 首先将响应收集到一个字符串中:

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder fullResponse = new StringBuilder();
String str;
while ((str = in.readLine()) != null) {
  fullResponse.append(str);
}

Then, if the string starts with "{", you can use: 然后,如果字符串以“ {”开头,则可以使用:

JSONObject obj = new JSONObject(fullResponse.toString()); //[1]

and if it starts with "[", you can use: 如果以“ [”开头,则可以使用:

JSONArray arr = new JSONArray(fullResponse.toStrin()); //[2]

[1] http://json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29 [1] http://json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29

[2] http://json.org/javadoc/org/json/JSONArray.html#JSONArray%28java.lang.String%29 [2] http://json.org/javadoc/org/json/JSONArray.html#JSONArray%28java.lang.String%29

Firstly, this is actually not a servlet problem. 首先,这实际上不是servlet问题。 You don't have any problems with javax.servlet API. javax.servlet API没有任何问题。 You just have problems with java.net API and the JSON API. 您只是在使用java.net API和JSON API时遇到问题。

For parsing and formatting JSON strings, I would recommend to use Gson (Google JSON) instead of the legacy JSON API's. 为了解析和格式化JSON字符串,我建议使用Gson (Google JSON)而不是传统的JSON API。 It has much better support for generics and nested properties and can convert a JSON string to a fullworthy javabean in a single call. 它对泛型和嵌套属性有更好的支持,并且可以在一次调用中将JSON字符串转换为完全有价值的javabean。

I've posted a complete code example before here . 之前,我已经发布了完整的代码示例。 Hope you find it useful. 希望你觉得它有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM