karate - 如何在空手道中找到json数组名称有空格的json键值
问题描述
我需要在 json 中声明一个数组元素,其中数组名称中有空格
示例 json:
{
"integration": {
"message": {
"code": 0,
"description": "Success"
},
"serviceData": {
"TenantID": "1909627861",
"Company Name": "anupamaCompany",
"Sub Domain": "testadd",
"Usage Data": [{
"Stage": "Test",
"Service Type": "OrchestrationIntegration",
"Integration Name": "Add",
"Applications List": [],
"createdBy": "Anupama2",
"Created Date": "2018-10-03",
"Last ModifiedBy": "Anupama2",
"Last Modified Date": "2019-01-10",
"Integration Type": "Light"
}, {
"Stage": "Development",
"Service Type": "OrchestrationIntegration",
"Integration Name": "Xzxzx",
"Applications List": [],
"createdBy": "Anupama2",
"Created Date": "2019-01-11",
"Last ModifiedBy": "Anupama2",
"Last Modified Date": "2019-01-11",
"Integration Type": "Light"
}]
}
}
}
要断言的值:
integration.serviceData.["Usage Data"].Stage
当我遍历“使用数据”时出现错误,因为它有空间。
解决方案
您使用空格访问密钥的方法是正确的,但是您似乎忘记了“使用数据”中的数据,它是数组/列表类型的数据。
因此您必须通过索引或 json 深度扫描或许多其他选项来访问它。
这里有几个,
# to get all usage data as list
* def usageData = $example.integration.serviceData.['Usage Data']
# to get all stage in usage data
* def stages = $example.integration.serviceData.['Usage Data']..Stage
推荐阅读
- python - 如何比较数据框两列中的值
- docusignapi - 文档 | 嵌入式签名 | 发件人如何与收件人/签名者共享“收件人签名 URL”?
- inno-setup - 使用 PrivilegesRequired=admin 从浏览器运行 InnoSetup 安装程序
- sql - 在检查是否需要保留旧值或插入新值时在 postgres SQL 上进行 Upsert 查询?
- java - NavigationComponent 与 BottomNavigationView 项目选择
- javascript - 这样使用flexbox可以吗?
- html - Vue项目中的Bootstrap datepicker显示问题
- javascript - 是否可以将一个功能“分配”给另一个功能?
- javascript - CSSTransition transition-enter-active 事件未按预期工作
- powershell - 将 Azure AD 用户添加到特定的安全组(异常处理)