regex - 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
}
]
解决方案
您的响应是 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
推荐阅读
- mysql - 我的 SQL 中存在日期和日期时间比较问题
- user-interface - Pentaho Report Designer 的 UI 太小
- c# - 部署时Winform应用程序重新启动不起作用
- reactjs - 如何将 ant.design 跨度重置为默认值?
- javascript - 使用 p5.js 在 JavaScript 中未定义 JSON 属性
- java - 将 JButton 添加到 JPanel 到 JFrame
- c# - 在 DbUpdateConcurrencyExpcetion 上实现乐观并发
- python - 如何正确处理类的属性变化?
- git - CVS2GIT-转换:运行 git fast-import 时出现错误“标记...未声明”
- html - css3过渡变换让元素之间的空白