如何使用正则表达式提取器或任何其他提取器(Xpath、Beanshell 等)从下面给出的 Json 中提取所有 ID 值

How to extract all the Id's value from the Json given below using Regular expression extractor or any other Extractor(Xpath, Beanshell etc)

[
    {
        "id": 1,
        "name": "MetaOperationN1",
        "type": "Operation"
    },
    {
        "id": 2,
        "name": "GreenOper2",
        "type": "Operation"
    },
    {
        "id": 3,
        "name": "GreenOper4",
        "type": "Operation"
    },
    {
        "id": 4,
        "name": "GreenOper5",
        "type": "Operation"
    },
    {
        "id": 5,
        "name": "GreenOper6",
        "type": "Operation"
    },
    {
        "id": 6,
        "name": "GreenOper7",
        "type": "Operation"
    },
    {
        "id": 7,
        "name": "GreenOper8",
        "type": "Operation"
    },
    {
        "id": 8,
        "name": "GreenOper9",
        "type": "Operation"
    },
    {
        "id": 9,
        "name": "GreenOper10",
        "type": "Operation"
    },
    {
        "id": 10,
        "name": "GreenOper11",
        "type": "Operation"
    }
]

不要使用正则表达式解析 json 文档.. 通过参考 this

轻松完成

如果您想使用正则表达式..继续:

"id": (\d+)

并使用

提取 ID
  1. 使用具有以下配置的正则表达式提取器:

    • 参考名称:任何有意义的,即ID
    • 正则表达式:"id": (\d+),
    • 模板:$
    • 匹配号:-1
  2. 使用JSON Path Extractor (available via JMeter Plugins)

    • 参考名称:随便你,即ID
    • JSON路径表达式:$..id

在这两种情况下,您将获得如下 ID 列表:

ID_1=1
ID_10=10
ID_2=2
ID_3=3
ID_4=4
etc.

有关安装 JSON 路径提取器和 JSON 路径语言的更多信息,请查看 Using the XPath Extractor in JMeter(向下滚动至 "Parsing JSON")