使用键在 JSonPath 中断言数组对象

Asserting Array objects in JSonPath with key

我试图通过内容键而不是数组位置来获取特定的数组项。

示例JSON:

{"items": [{
        "key": "prop-name",
        "label": "Provider",
        "type": "DROPDOWN",
        "items": ["bla", "blub", "what", "ever"]
    }, {
        "key": "prop-modes",
        "label": "Transport modes",
        "type": "CHECKBOX",
        "items": ["AIR", "RAIL", "ROAD", "SEA"]
    }
]}
private static String URL_GLOBAL_FILTER = "/global-filter";
private static String PROP_NAME = "prop-name";

mvc.perform(get(URL_GLOBAL_FILTER).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
   .andExpect(jsonPath("$.items", hasSize(10)))
   .andExpect(jsonPath("$.items[?((@.key === \"" + PROP_NAME + "\" && @.type === \"DROPDOWN\"))]").exists())
   .andExpect(jsonPath("$.items[0].items", hasSize(4)));

这很好用。但我想做的是:

.andExpect(jsonPath("$.items[?(@.key == \"prop-name\")].items", containsString("bla")))

但这会引发以下错误:

java.lang.AssertionError: JSON path "$.items[?(@.key == "prop-name")].items"

应为:包含 "bla" 的字符串 但是:是 net.minidev.json.JSONArray (<[["bla","blub","what","ever"]]>)

< > 是什么意思?如何访问里面的数组?

jsonQueryTool看来是我想要的

(更新问题)

您是否尝试过使用 Hamcrest 的 hasItem 匹配器?

import static org.hamcrest.Matchers.*;

.andExpect(jsonPath("$.items[?(@.key == 'prop-name')].items[*]", hasItem("bla")));