首页 > 解决方案 > 如何在空手道中找到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

当我遍历“使用数据”时出现错误,因为它有空间。

标签: karate

解决方案


您使用空格访问密钥的方法是正确的,但是您似乎忘记了“使用数据”中的数据,它是数组/列表类型的数据。

因此您必须通过索引或 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

推荐阅读