简体   繁体   English

如何解析获取请求响应?

[英]How to parse Get Request Response?

I am attempting to parse through json data and I have come across an issue where it is not parsing anything as well as not being output into my Table row object.我正在尝试解析 json 数据,但遇到了一个问题,即它没有解析任何内容,也没有输出到我的 Table 行对象中。

I have created a parse method :我创建了一个解析方法:

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

Here is my Http 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();
            }

        }

Here is my result :这是我的结果:

 {"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}]

but the expected output should the json data being parse :但预期的输出应该解析 json 数据:

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

Any advice to proceed?有什么建议可以继续吗?

There are many JSON parsers for Java, eg, Jackson , and fastjson , so not need to write JSON parser by yourself. Java 有很多 JSON 解析器,例如Jacksonfastjson ,所以不需要自己编写 JSON 解析器。

and for HTTP request, okhttp is a good choice.而对于 HTTP 请求, okhttp是一个不错的选择。

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

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