gpt4 book ai didi

json - 如何断言 json 路径值大于值?

转载 作者:行者123 更新时间:2023-12-05 04:47:37 26 4
gpt4 key购买 nike

给定以下从 REST 调用返回的 JSON:

{"Statistik Eintraege":
[{"Wert":"1","Anzahl":41},
{"Wert":"","Anzahl":482},
{"Wert":"-3","Anzahl":1},
{"Wert":"-1","Anzahl":3},
{"Wert":"-2","Anzahl":3}],
"Statistik Typ":"BlahStatistik"}

...我想验证 Wert='' 的 'Anzahl' 是否大于 400(在本例中为:482)。

我在 java 集成测试中尝试的是:

.andExpect(jsonPath("$..[?(@.Wert == '')].Anzahl", greaterThan(400)));

The exception:

java.lang.ClassCastException: class net.minidev.json.JSONArray cannot be cast to class java.lang.Comparable (net.minidev.json.JSONArray is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')

at org.hamcrest.comparator.ComparatorMatcherBuilder$1.compare(ComparatorMatcherBuilder.java:22)
at org.hamcrest.comparator.ComparatorMatcherBuilder$ComparatorMatcher.describeMismatchSafely(ComparatorMatcherBuilder.java:86)
at org.hamcrest.TypeSafeMatcher.describeMismatch(TypeSafeMatcher.java:82)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$value$0(JsonPathResultMatchers.java:87)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at

我还能尝试什么?

最佳答案

JsonPath 运算符 [?(<expression>)]选择与给定表达式匹配的所有元素。因此,结果是一个json数组。

在例子中[?(@.Wert == '')]将所有 json 节点与字段 Wert 匹配有一个空值。您的 json 示例只有与谓词匹配的单个项目,但通常可能有多个。要解决这个问题,您要么定义一个更具体的表达式,只匹配一个元素,要么调整匹配器以处理集合。

匹配集合:

.andExpect(jsonPath("$..[?(@.Wert == '')].Anzahl", everyItem(greaterThan(400))))

关于json - 如何断言 json 路径值大于值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68410880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com