【问题标题】:how to find json key value where json array name has space in karate如何在空手道中找到json数组名称有空格的json键值
【发布时间】:2019-01-18 19:34:29
【问题描述】:

我需要在 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


【解决方案1】:

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

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

【讨论】:

  • 回复非常快速准确
  • 很高兴我能帮上忙 :)
  • 实际上在 0.9.0 github.com/intuit/karate/issues/652 中引入了一个错误 - @BabuSekaran 的解决方法目前是正确的
  • 哎呀!看起来只有匹配* def Stages = $example.integration.serviceData.['Usage Data'][0].Stage 的问题仍然完好无损。很棒的收获@PeterThomas
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多