【问题标题】:Validate value of specific key which returns list using JsonPath使用 JsonPath 验证返回列表的特定键的值
【发布时间】:2026-01-25 09:55:02
【问题描述】:

响应正文:

{
 "Items":[{
            "ID": 12,
            "Name": "n1",
            "Inactive": false
        },
        {
            "ID": 16,
            "Name": "n2",
            "Inactive": false
        },  ...etc
        ]
}

使用 JsonPath 库,我能够通过以下表达式获取特定元素的所有值(例如“非活动”)的列表。 '$.. 不活动' ---> [false, false,..etc]

但是,我不确定如何使用 JsonPathAssert 应用匹配器来断言上述列表,以便它应该只包含“false”。

我正在使用 Java,JsonPath。有人可以帮我吗?

【问题讨论】:

    标签: java json assertions hamcrest jsonpath


    【解决方案1】:

    经过一些研究,我能够断言该列表 - 应该只包含下面提到的“false”。

    org.hamcrest.MatcherAssert.assertThat(actualJson.toString(), JsonPathMatchers.hasJsonPath("$..Inactive", Matchers.everyItem(Matchers.equalTo(false))));
    

    【讨论】:

      最近更新 更多