首页 > 解决方案 > JMeter如何使用正则表达式提取器从响应中的项目数组中提取ID

问题描述

我完全是 JMeter 的新手。我有与 JMeter 相关的类似问题:Regex extracting from an array response 这里我使用相同的测试有效负载。我正在尝试使用正则表达式提取器并想提取所有 addressId。即[0011442239, 0011442246]。我有一个要发送的发布请求,有效负载只是 addressIds 数组。谁能帮我解决这个问题?

我尝试使用\[([^\]]+)\] 但这没有用。正如我上面提到的,我对 JMeter 和正则表达式完全陌生。

抱歉,我的错误是我期望响应为整数数组。即[0011442239, 0011442246]。

谢谢

[
  {
    "footprint": null,
    "type": null,
    "addressId": "0011442239",
    "streetName": "solitudestr.",
    "streetNrFirstSuffix": null,
    "streetNrFirst": null,
    "streetNrLastSuffix": null,
    "streetNrLast": null,
    "houseNumber": "25",
    "houseName": null,
    "city": "stuttgart",
    "postcode": "70499",
    "stateOrProvince": null,
    "countryName": null,
    "poBoxNr": null,
    "poBoxType": null,
    "attention": null,
    "geographicAreas": [
      
    ],
    "firstName": null,
    "lastName": null,
    "title": null,
    "region": "BW",
    "additionalInfo": null,
    "properties": [
      
    ],
    "extAddressId": null,
    "entrance": null,
    "district": null,
    "addressLine1": null,
    "addressLine2": null,
    "addressLine3": null,
    "addressLine4": null,
    "companyName": null,
    "contactName": null,
    "houseNrExt": null,
    "derbyStack": false
  },
  {
    "footprint": null,
    "type": null,
    "addressId": "0011442246",
    "streetName": "solitudestr.",
    "streetNrFirstSuffix": null,
    "streetNrFirst": null,
    "streetNrLastSuffix": null,
    "streetNrLast": null,
    "houseNumber": "26",
    "houseName": null,
    "city": "stuttgart",
    "postcode": "70499",
    "stateOrProvince": null,
    "countryName": null,
    "poBoxNr": null,
    "poBoxType": null,
    "attention": null,
    "geographicAreas": [
      
    ],
    "firstName": null,
    "lastName": null,
    "title": null,
    "region": "BW",
    "additionalInfo": null,
    "properties": [
      
    ],
    "extAddressId": null,
    "entrance": null,
    "district": null,
    "addressLine1": null,
    "addressLine2": null,
    "addressLine3": null,
    "addressLine4": null,
    "companyName": null,
    "contactName": null,
    "houseNrExt": null,
    "derbyStack": false
  }
]

标签: regexjmeter

解决方案


您的响应是 JSON,因此您应该考虑使用JSON JMESPath Extractor,它允许执行任意 JMESPath 查询,而不是尝试使用正则表达式解析它。

特别是您的情况,您可以使用以下表达式得出这些 addresIds 的逗号分隔列表:

[*].addressId | join(',',@)

演示:

在此处输入图像描述

如果您确实需要用引号将这些 addressId 括起来,则需要切换到JSR223 PostProcessor和以下 Groovy 代码:

com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(),'$..addressId').collect {it -> "\"$it\""} as String

在此处输入图像描述


推荐阅读