如何解析“获取请求响应”?

时间:2019-08-05 00:50:09

标签: java json tablerow get-request

我正在尝试解析json数据,但遇到一个问题,即它不解析任何内容,也没有输出到我的Table行对象中。

我创建了一个解析方法:

  public static String parseRequest(String request,Table Row) {
            String array1 [] = request.split(":");
            for (int i = 0; i < array1.length; i++) {
                String[] array2 = array1[i].split(",");
            }
            return request;
        }

这是我的Http请求:

public static void GetRequest() {

            BufferedReader reader;
            String access_token = "BlahBlahBlach";      

            String line;
            StringBuffer responseContentReader = new StringBuffer();

            try {

                URL url = new URL("https://bigquery.googleapis.com/discovery/v1/apis/bigquery/v2/rest");
                connection = (HttpsURLConnection) url.openConnection();

                connection.setRequestProperty("X-Risk-Token ", access_token);


                connection.setRequestProperty("Accept", "application/json");

    //here we should be able to "request" our setup
    //Here will be the method I will use 
                connection.setRequestMethod("GET");

    //after 5 sec if the connection is not successful time it out
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);

                int status = connection.getResponseCode();
    //System.out.println(status); //here the connect was established  output was 200 (OK)

    //here we are dealing with the connection isnt succesful

                if (status > 299) {
                    reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                    while ((line = reader.readLine()) != null) {
                        responseContentReader.append(" ");
                        responseContentReader.append(line);
                        responseContentReader.append("\n");
                    }
                    reader.close();
    //returns what is successful    
                } else {
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    while ((line = reader.readLine()) != null) {
                        responseContentReader.append(" ");
                        responseContentReader.append(line);
                        responseContentReader.append("\n");
                    }
                    reader.close();
                }
                //System.out.println(responseContentReader.toString());
                System.out.println(parseRequest(responseContentReader.toString(),row);


            } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
    // TODO: handle exception
                e.printStackTrace();
            } finally {
                connection.disconnect();
            }

        }

这是我的结果:

 {"vulnerability":{"id":6202813,"status":"open","closed_at":null,"created_at":"2019-06-18T19:45:13Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-reader-apsb10-28-cve-2010-3654"],"last_seen_time":"2019-07-30T05:00:00.000Z","fix_id":7109,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-reader-apsb10-28-cve-2010-3654","open":true}]

但是预期的输出应该是解析的json数据:

 id : 6202813
    status: open
    closed_at: null
    etc..

有建议继续吗?

1 个答案:

答案 0 :(得分:1)

Java有许多JSON解析器,例如Jacksonfastjson,因此不需要自己编写JSON解析器。

对于HTTP请求,okhttp是一个不错的选择。