简体   繁体   中英

How can I parse the JSON response and could get value from known key as output?

My code is as below, I tried using JSONOption but it did not work

Response res =  (Response) RestAssured.given().queryParam("CUSTOMER_ID","68195").queryParam("PASSWORD","1234!").queryParam("Account_No","1").get("http://demo.guru99.com/V4/sinkministatement.php").then().extract().response();
System.out.println(res.body().asString());
        
        JSONObject jsonBody = new JSONObject(res.prettyPrint());
       System.out.println(jsonBody);

I can get JSON Body with PreetyPrint method but could not fetch key-value pair from that response. Seems like I need to parse output of body method.

There is a command tool called JQ and JS implementation at https://github.com/fiatjaf/jq-web .

Sample usage:

var jq = require('jq-web')
var result = jq.json({...}, 'select(.CUSTOMER_ID="123")');

Try HttpClient's GetMethod.

private static HttpClient httpClient;
       
    public void getTest() {
          GetMethod request = new GetMethod("http://demo.guru99.com/V4/sinkministatement.php" +
                "?CUSTOMER_ID=68195" + 
                "&PASSWORD=1234" +
                "&Account_No=1");
            try {
                int result = httpClient.executeMethod(request);
                String responseContent = request.getResponseBodyAsString();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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