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

时间:2015-05-23 12:09:01

标签: regex json xpath jmeter

[
    {
        "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"
    }
]

2 个答案:

答案 0 :(得分:1)

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

    • 参考名称:任何有意义的内容,即ID
    • 正则表达式:"id": (\d+),
    • 模板:$1$
    • 比赛编号:-1
  2. 使用JSON Path Extractor(可通过JMeter Plugins获得)

    • 参考名称:您想要的任何内容,即ID
    • JSONPath Expression:$..id
  3. 在这两种情况下,您都会将ID列表作为:

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

    有关安装JSON Path Extractor和JSON Path语言的更多信息,请查看Using the XPath Extractor in JMeter(向下滚动到"解析JSON")

答案 1 :(得分:0)

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

轻松完成

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

"id": (\d+)

使用$1

提取出ID