Gatling:正则表达式从 JSON 中获取特定值

Gatling : regex to get a specific value from a JSON

我有这个 JSON 对象来自 HTML。

{
  "isCompany": false,
  "accommodations": [
    {
      "id": "00000000031000006435",
      "isChecked": false,
      "name": "Théo",
      "addressLine1": "Rue des patriotes 40 Y",
      "addressLine2": "1050 Ixelles",
      "nightsDeclared": 5,
      "schoolNightsDeclared": 3,
      "schoolNightsAttached": 0,
      "taxableNights": 2.0,
      "totalPayment": 0.0,
      "isInProgress": true,
      "isLate": false,
      "isPayed": "false",
      "deadline": "2021-12-31",
      "initialAmount": 0.0,
      "remainingAmount": 0.0
    },
    {
      "id": "00000000031000006438",
      "isChecked": false,
      "name": "Théo",
      "addressLine1": "Rue des Gens 45 B",
      "addressLine2": "1040 Etterbeek",
      "nightsDeclared": 5,
      "schoolNightsDeclared": 3,
      "schoolNightsAttached": 0,
      "taxableNights": 2.0,
      "totalPayment": 0.0,
      "isInProgress": true,
      "isLate": false,
      "isPayed": "false",
      "deadline": "2021-12-31",
      "initialAmount": 0.0,
      "remainingAmount": 0.0
    }
  ]
}

我知道在 Gatling 中,可以通过编写此正则表达式来获取住宿 ID :

check(regex(""""accommodations":\[\{"id":"(.*?)"""").saveAs("accommodationId"))

现在我的问题是,获得 "isInProgress" 的正则表达式是什么?

好的,你的正则表达式中有这个

"id":"(.*?)"

您只需将预期的键名更改为 isInProgress 或任何其他名称。还要注意 (.*?) 周围的 " - 因为 id 的值是一个字符串,所以需要它们,但是 isInProgress 中的值与另一个 type.

不要!

在这种特定情况下使用正则表达式可能会导致您的代码因轻微的输入更改而中断,并导致代码不可读。

反序列化并作为字典访问?

[a['id'] for a in json.loads(json_string)['accommodations']]

此外,您是否尝试过将 id 简单地替换为您想要的字段名称?

如果您坚持为此使用正则表达式,请查看 regex101.com、regexr.com、regextester.com 等在线正则表达式网站(搜索“在线正则表达式测试”)并尝试自己解决这个问题。如果您的代码不起作用,请再次提问。