【问题标题】:How to get particular json key value from Response Object如何从响应对象中获取特定的 json 键值
【发布时间】:2017-05-02 05:14:09
【问题描述】:

通过 RestAssured API 调用后,我将 REST API 的响应转换为 Response 对象。

响应体是json,我想从中获取特定的键值吗?

下面是代码

Response res = given()
             .relaxedHTTPSValidation()
             .with()
             .contentType(ConfigReader.get("application.json")) 
             .then()
             .get(url);

String rbody = res.body().asString();

如何获取rbody字符串中的特定键值?

【问题讨论】:

    标签: rest-assured


    【解决方案1】:

    Response 类有方法path() 使用它,用户可以给出json路径来获取特定值。

    例如:-

    Response res = given()
                 .relaxedHTTPSValidation()
                 .with()
                 .contentType(ConfigReader.get("application.json")) 
                 .then()
                 .get(url);
    String value = res.path("root.childKey").toString();
    

    root.childKey是json元素的路径

    【讨论】:

      【解决方案2】:

      作为 Restassured 一部分的 JasonPath 类是我在项目中使用的类。首先,您需要使用以下命令导入 JsonPath 类:

      import com.jayway.restassured.path.json.JsonPath;
      

      然后您需要传递 JSON 字符串并使用它来创建 JsonPath 对象。从 JsonPath 对象中,您可以使用键来获取相应的值。以下代码将为您工作。

      Response res = given()
               .relaxedHTTPSValidation()
               .with()
               .contentType(ConfigReader.get("application.json")) 
               .then()
               .get(url);
      
      String rbody = res.asString();
      JsonPath jp = new JsonPath( rbody );
      String value = jp.getString( "your.key" );
      

      【讨论】:

        【解决方案3】:

        JSON 的格式类似于 {someProprty:"someValue"},因此您不想将其作为字符串获取,而是要访问该特定属性。即:b.body.someProperty

        注意:我强烈建议您将回复命名为更像resresponse。你不会喜欢有b 作为你的回复。

        How to access JSON Object name/value?

        JSON 也可以像 {somePropertyThatIsNumerical:1} 那样格式化,也可以包含数组。

        【讨论】:

        • 嗨,格伦,感谢您的回答和建议。我没有任何方法可以直接将 json 密钥作为res.body.someProperty。我检查了它有body() or getbody() 方法,然后直接访问密钥,我在Response 类中没有找到任何方法。
        • 嗨,格伦,我找到了答案,我们可以使用 res.path() 函数,例如:- b.path("config"),谢谢
        【解决方案4】:
        baseURI="url";
                Map<String,String> reqParam=new HashMap<String,String>();
                reqParam.put("loginID","abc");
                reqParam.put("password","123");
        
                JSONObject reqObjects=new JSONObject(reqParam);
                Response response = 
                given()
                  .header("Content-Type", "application/json").accept(ContentType.JSON)
                  .when()
                  .body(reqObjects.toJSONString()).post("/v1/getDetails")
                  .then().log().body().extract().response();
        
                 String responseBody= response.asString();
                 JsonPath path=new JsonPath(responseBody);
                 String key=path.getString("path.key");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          • 2017-02-15
          相关资源
          最近更新 更多