【问题标题】:In restassurred, how to get particular attribute value using its sibling attribute value in json response of api?放心,如何在api的json响应中使用其兄弟属性值获取特定属性值?
【发布时间】:2020-04-12 11:51:08
【问题描述】:

以下是我的回复 { “身份证”:123, “名称”:“文本 1” }, { “身份证”:456, “名称”:“文本2” } ]

我想查找名称为 'text2' 的 id 的值

【问题讨论】:

  • JsonPath FTW - $..[?(@.name == 'text2')].id,使用 JSONPath Validator 验证
  • @WilfredClement Rest-Assured 不使用 json-path。它行不通。放心使用Groovy's GPath

标签: json api rest-assured rest-assured-jsonpath


【解决方案1】:

由于 Rest Assured 使用 Groovy 的 GPath,您可以使用以下表达式:

String json = "[{ \"id\": 123, \"name\": \"text1\" }, { \"id\": 456, \"name\": \"text2\" }]";
JsonPath path = JsonPath.from(json);
System.out.println(path.get("find { it.name == 'text2' }.id"));

以上代码将返回456

仅当 JSON 以数组 [] 开头时才有效

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多