【问题标题】:Get value from json based on condition using JsonPath使用 JsonPath 根据条件从 json 中获取值
【发布时间】:2020-03-26 06:18:50
【问题描述】:

我已经关注 json -

{
"issues": [{
    "id": 200615,
    "custom_fields": [{
        "id": 166,
        "name": "Severity",
        "internal_name": null,
        "field_format": "list",
        "value": "Major"
    }, {
        "id": 167,
        "name": "Reproducibility",
        "internal_name": null,
        "field_format": "list",
        "value": "Always"
    }, {
        "id": 168,
        "name": "Resolution",
        "internal_name": null,
        "field_format": "list",
        "value": "open"
    }]
}]

}

在这个 json 中,我想获取问题的值 -> custome_fields -> id 为 167 且预期值为 - 始终的值(167 可以位于 json 中的任何位置,因此无法对其进行硬编码)

请问我怎样才能实现价值?

jsonPath.get("issues[0].custom_fields[?(@.id == '167')].value[0]")

导致错误 -

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 50.
   Object.issues[0].custom_fields[?(@.id ==

【问题讨论】:

    标签: rest-assured jsonpath rest-assured-jsonpath


    【解决方案1】:

    我遇到了和你一样的错误,我不知道为什么。

    但是,您可以使用findAll json 路径方法实现相同的目标,如下所示:

    issues[0].custom_fields.findAll { it.id == 167 }.value[0]
    

    打印出来:Always

    【讨论】:

    • 谢谢!或者我们可以find 得到个人喜欢issues[0].custom_fields.find { it.id == 24 }.value
    猜你喜欢
    • 2014-06-29
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2019-02-06
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多