Jmeter groovy返回Json中的一组ID
我的HTTP响应如下所示:Jmeter groovy返回Json中的一组ID,groovy,jmeter,Groovy,Jmeter,我的HTTP响应如下所示: { "Result": [ { "xPath": "/BB[001]", "name": "Block001", "folder": "\\", "id": 13, "information": [ { "xPath": "/BB[001]", "result": "BB1" } ], "error
{
"Result": [
{
"xPath": "/BB[001]",
"name": "Block001",
"folder": "\\",
"id": 13,
"information": [
{
"xPath": "/BB[001]",
"result": "BB1"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[001]",
"name": "I_TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 146,
"information": [
{
"xPath": "/TestCases/TestCase[001]",
"result": "Test1: TT1"
},
{
"xPath": "/TestCases/TestCase[001]",
"result": "Folder path: \\Automation-Inbnd\\TT"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[002]",
"name": "TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 147,
"information": [
{
"xPath": "/TestCases/TestCase[002]",
"result": "Test Case Number 2TTO"
}
],
"error": []
}
]
}
$..[?(@.folder =~ /\\Automation-Inbnd\\.*?/i)].id
在Groovy JSR223后处理器中,我只想在->folder:\Automation Inbnd\TT之后提取那些ID,所以在本例中,我只想提取146,而不是147,而不是13
一个解决所有入侵检测的方案
"folder": "\\Automation-Inbnd\\TT",
如果我们甚至可以使TT变量,这将是伟大的,因为我有另一个测试用例,它使用不同的子文件夹
所以所有的ID只有在
"folder": "\\Automation-Inbnd\\(*)",
哪里有东西,然后
我感谢你的帮助,因为我在这方面花了很多时间。谢谢您应该能够使用JsonSlurper:
def ids = new groovy.json.JsonSlurper()
.parseText(response)
.Result
.findAll { it.folder == '\\Automation-Inbnd\\TT' }
.id
作为替代解决方案,您可以使用Json提取器后处理器,Jsonpath如下所示:
{
"Result": [
{
"xPath": "/BB[001]",
"name": "Block001",
"folder": "\\",
"id": 13,
"information": [
{
"xPath": "/BB[001]",
"result": "BB1"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[001]",
"name": "I_TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 146,
"information": [
{
"xPath": "/TestCases/TestCase[001]",
"result": "Test1: TT1"
},
{
"xPath": "/TestCases/TestCase[001]",
"result": "Folder path: \\Automation-Inbnd\\TT"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[002]",
"name": "TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 147,
"information": [
{
"xPath": "/TestCases/TestCase[002]",
"result": "Test Case Number 2TTO"
}
],
"error": []
}
]
}
$..[?(@.folder =~ /\\Automation-Inbnd\\.*?/i)].id
完整配置如下所示:
{
"Result": [
{
"xPath": "/BB[001]",
"name": "Block001",
"folder": "\\",
"id": 13,
"information": [
{
"xPath": "/BB[001]",
"result": "BB1"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[001]",
"name": "I_TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 146,
"information": [
{
"xPath": "/TestCases/TestCase[001]",
"result": "Test1: TT1"
},
{
"xPath": "/TestCases/TestCase[001]",
"result": "Folder path: \\Automation-Inbnd\\TT"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[002]",
"name": "TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 147,
"information": [
{
"xPath": "/TestCases/TestCase[002]",
"result": "Test Case Number 2TTO"
}
],
"error": []
}
]
}
$..[?(@.folder =~ /\\Automation-Inbnd\\.*?/i)].id
这将生成一组变量id_1,id_2,例如,对于您的样本,它将如下所示:
{
"Result": [
{
"xPath": "/BB[001]",
"name": "Block001",
"folder": "\\",
"id": 13,
"information": [
{
"xPath": "/BB[001]",
"result": "BB1"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[001]",
"name": "I_TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 146,
"information": [
{
"xPath": "/TestCases/TestCase[001]",
"result": "Test1: TT1"
},
{
"xPath": "/TestCases/TestCase[001]",
"result": "Folder path: \\Automation-Inbnd\\TT"
}
],
"error": []
},
{
"xPath": "/TestCases/TestCase[002]",
"name": "TT",
"folder": "\\Automation-Inbnd\\TT",
"id": 147,
"information": [
{
"xPath": "/TestCases/TestCase[002]",
"result": "Test Case Number 2TTO"
}
],
"error": []
}
]
}
$..[?(@.folder =~ /\\Automation-Inbnd\\.*?/i)].id
到目前为止,您尝试了什么?我尝试了使用JSON提取器字符串[]Ids=vars.getid_ALL.split,;但它给了我所有的id,即使是13I也尝试了->字符串结果=响应.substring0,响应.indexOf'Automation-Inbnd',但在itI也尝试了def matcher=response=~'文件夹:\\\\\Automation Inbnd\\\\TT,id:\\d+,'if matcher.find{System.out.println'folder id='+matcher.group1}之后,却无法找到id值另外->def matcher=response=~'folder\\\\\\\\\TT,id:\\d+,'在JSR223后处理器中,我尝试了您的解决方案,并添加了->System.out.println'ids------'+ids我得到了id------[],所以在我的后处理器中我得到了def id=new groovy.json.JsonSlurper.parseTextresponse.Result.findAll{it.folder='\\Automation Inbnd\\TT'}.id System.out.println'ids------'+ids我在ids上什么都没有得到我假设你在问题中的示例JSON与你正在检查的不同谢谢你@Tim我只是不理解你的解决方案,主要是因为我对groovy不太了解。谢谢你的帮助谢谢你@Kiri非常优雅的解决方案->美元。。[?@.folder=~/\\Automation-Inbnd\\\.*?/i].id我喜欢它,它很管用