java - How to check whether all elements of a JSON array start with a specific string?
问题描述
The input data looks as follows.
[
{
"mahKey": "mahString.yolo",
"mahValue": "0"
},
{
"mahKey": "notMahString.crymeariver",
"mahValue": "1"
},
{
"mahKey": "mahSTring.omg",
"mahValue": "2"
}
]
So I have this.
resultActions
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$.*", isA(ArrayList.class)))
.andExpect(jsonPath("$.[mahKey]", Matchers.everyItem(Matchers.startsWith("mahString"))));
The array after $.[mahKey]
looks like this.
[
"mahString.yolo",
"notMahString.crymeariver",
"mahSTring.omg"
]
However, I get the following error:
com.jayway.jsonpath.InvalidPathException: Could not parse token starting at position 3. Expected ?, ', 0-9, *
How to check whether all elements of a JSON array start with a specific string in JUnit?
解决方案
This should do it:
"$.*.mahKey"
So the syntax would look like:
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$.*", isA(ArrayList.class)))
.andExpect(jsonPath("$.*.mahKey", Matchers.everyItem(Matchers.startsWith("mahString"))));
Tested here, it retrieved the elements correctly:
推荐阅读
- reactjs - 如何一次将道具传递给所有样式化的组件css属性?
- authentication - 在 Nativescript App 中使用 Axios 进行用户认证
- flutter - 你能改变 IntelliJ Dart 结束评论的颜色吗?
- laravel - 在 laravel 中处理对象的集合
- swiftui - 视图内容更改时如何阻止 SwiftUI DragGesture 死机
- javascript - 使标题在除登录之外的所有组件上可用
- azure-keyvault - get-azurekeyvaultkey 不返回“公钥”
- c# - 仅在一侧与导航属性的一对多关系?
- string - Rust 中的字符串引用比较
- javascript - 从数组中随机抓取 3 个项目,其中第 3 个项目可以是“餐厅”,但不一定是