每次运行代码时,我都需要从下面的json中选择任意随机值。我如何在空手道中实现这一目标?我需要获取任何随机值并在另一个功能文件中使用。


[
  {
    "derivativeId": "BL33F1-2020-MASMGM2-GPKBPKB-GPSAPSA-GPS1PS1-GPTEPTE-GPU2PU2-GWW2WW2-GYOBYOB"
  },
  {
    "derivativeId": "1233AV-2020-GPJCPJC-GPRAPRA-GYOIYOI-MLSE8RL"
  },
  {
    "derivativeId": "1239TX-2020-GYOIYOI"
  },
  {
    "derivativeId": "1233AV-2020-GPJCPJC-GPRAPRA-MSNRSA6-GW4CW4C-GYOIYOI-MLSE8RL"
  },
  {
    "derivativeId": "1233AV-2020-GPDWPDW-GPRAPRA-GW4CW4C-GYOIYOI-MLSE8RL"
  }
]

最佳答案

在这里,您还可以参考:Karate - Select a random element from json

* def random = function(max){ return Math.floor(Math.random() * max) }
* def index = random(response.length)
* def result = response[index].derivativeId

10-08 19:30