【问题标题】:Karate - Select a random element from json空手道 - 从 json 中选择一个随机元素
【发布时间】:2018-12-29 19:28:21
【问题描述】:

每次运行代码时,我都需要从下面的示例 json 中选择任何随机值。我怎样才能在空手道中做到这一点?我需要获取任何随机值并在另一个功能文件中使用。

def myJson = 
"""
{
      "sampleJson": {
        "random1": "1",
        "random2": "2",
        "random3": "3",
        "random4": "4",
        "random5": "5"
      }
  }
"""

【问题讨论】:

标签: karate


【解决方案1】:

另请参阅:https://github.com/intuit/karate#commonly-needed-utilities

* def random = function(max){ return Math.floor(Math.random() * max) + 1 }
* def myJson = 
"""
{
  "sampleJson": {
    "random1": "1",
    "random2": "2",
    "random3": "3",
    "random4": "4",
    "random5": "5"
  }
}
"""
* def key = 'random' + random(5)
* def result = myJson.sampleJson[key]
* print result

另见:https://stackoverflow.com/a/70376593/143475

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 2014-07-23
    • 2010-09-12
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    相关资源
    最近更新 更多