【问题标题】:How To Extract Json Data Filed With RestApi?如何使用 Rest Api 提取 Json 数据字段?
【发布时间】:2022-01-08 10:30:40
【问题描述】:

我放心地在 api 上发帖。而不是我试图确保来自响应数据的预期数据, 但是我遇到了一些这样的错误->“java.lang.IllegalArgumentException:参数“data”已使用但未定义。使用JsonPath.params(...)函数定义参数”


我的代码:

String payload_data = "{" +
                "\"Time\":1638057600, " +
                "\"exampleType\":example, " +
                "\"Id\":[2]}";

    RestAssured.defaultParser = Parser.JSON;
    given().
            contentType(ContentType.JSON).
            body(payload_data).
            when().
            post(api_url).
            then().
            statusCode(200).
            body("data.examples.2.exampleData", equalTo("33"));
}

我的 json 数据

{
    "success": true,
    "data": {
        "examples": {
            "2": {
                "ex_data": 0,
                "exampleData": 33,
                "data_ex": 0,
            }
      }
}

【问题讨论】:

标签: java api rest-assured jsonpath jsonparser


【解决方案1】:

首先,我用你的 json 测试了路径 "data.examples.2.exampleData",它工作正常。没问题。

你在这里犯了一些错误。

  1. 您的负载是无效的 json。
{
  "Time": 1638057600,
  "exampleType": example, //it must be number or String with double quote
  "Id": [
    2
  ]
}
  1. 您正在比较具有不同数据类型的 2 个事物。

"data.examples.2.exampleData" --> int 33

equalTo("33") --> 字符串“33”

修复:body("data.examples.2.exampleData", equalTo(33));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2017-10-12
    相关资源
    最近更新 更多