输入数据如下所示。
[
{
"mahKey": "mahString.yolo",
"mahValue": "0"
},
{
"mahKey": "notMahString.crymeariver",
"mahValue": "1"
},
{
"mahKey": "mahSTring.omg",
"mahValue": "2"
}
]
所以我有这个。resultActions
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$.*", isA(ArrayList.class)))
.andExpect(jsonPath("$.[mahKey]", Matchers.everyItem(Matchers.startsWith("mahString"))));
$.[mahKey]
之后的数组看起来像这样。[
"mahString.yolo",
"notMahString.crymeariver",
"mahSTring.omg"
]
但是,我收到以下错误:com.jayway.jsonpath.InvalidPathException: Could not parse token starting at position 3. Expected ?, ', 0-9, *
如何检查 JSON 数组的所有元素是否以 JUnit 中的特定字符串开头?
最佳答案
这应该这样做:
"$.*.mahKey"
所以语法看起来像:
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$.*", isA(ArrayList.class)))
.andExpect(jsonPath("$.*.mahKey", Matchers.everyItem(Matchers.startsWith("mahString"))));
Tested here ,它正确检索了元素:关于java - 如何检查 JSON 数组的所有元素是否以特定字符串开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66245551/