0

When I have arrays in response, getting proper results using Jayway, but not with io.restassured ? Can I use Jayway and io.restassured together ? Is that an acceptable / good practice?

JSON Response :

   {"applications": [
      {
      "Id": "123",
      "amount": "1500"
   },
      {
      "Id": "456",
      "amount": "2500"
   },
      {
      "Id": "780",
      "amount": "3500"
   }
]}

Looking for amount 2500 as my result! Tried below: //1st approach to read response form json body JsonPath jsonPath = res.jsonPath(); System.out.println(jsonPath.get("$.applications[1].amount")); //results null, using io.restassured JsonPath

//2nd approach to read response form json body JsonPath jsonPath1 = JsonPath.from(res.asString()); System.out.println(jsonPath1.getString("$.applications[1].amount")); //results null, using io.restassured JsonPath

//3rd approach to read response form json body System.err.println(JsonPath.read(res.asString(),"$.login")); // results 2500, using jaywayJsonPath

4

1 回答 1

1

有多种提取值的方法

    // Method 1
    String res = given().when().get("http://soapractice1.mocklab.io/thing/test").then().extract().asString();
    JsonPath js = new JsonPath(res);
    System.out.println("The amount is : " + js.get("applications[1].amount"));

    // Method 2
    Response resp = given().when().get("http://soapractice1.mocklab.io/thing/test").then().extract().response();
    JsonPath js1 = resp.jsonPath();
    System.out.println("The amount is : " + js.get("applications[1].amount"));

    // Method 3
    String amount = given().when().get("http://soapractice1.mocklab.io/thing/test").then().extract().jsonPath().get("applications[1].amount");
    System.out.println("The amount is : " + amount);
于 2020-07-27T14:10:22.497 回答