如何使用脚本从JSON格式获取响应?

时间:2012-06-18 17:23:34

标签: json groovy automation soapui

我正在使用SoapUI Pro 4.5.0,也是这个工具的初学者。我需要从响应中获取值(JSON格式)。任何人都可以给我示例代码吗?

注意:我使用的是基于REST的服务,而不是基于SOAP的服务。

2 个答案:

答案 0 :(得分:1)

SoapUI在groovy jar中包含JsonSlurper类,所以你只需要导入它就像

一样
import groovy.json.JsonSlurper

然后以

获得响应
responseContent = testRunner.testCase.getTestStepByName("<test_step_name>").getPropertyValue("Response")

jsonresponse = new JsonSlurper().parseTest(responseContent)

现在您可以像

一样访问响应中的元素
jsonresponse.id

答案 1 :(得分:0)

我建议使用Groovy脚本中的JSONPath。您可能需要将JSONPath Jar添加到/ bin / ext目录。

我没有SoapUI在我面前,但在groovy脚本中它会是这样的:

def getResponse = context.expand( '${SomeGet#response}' )
def jsonSuff = JsonPath.read(getResponse, "\$..*")

如果您在使用其他JAR时遇到问题(我在SoapUI中创建了一个外部JAR),这就是我的POM中的内容。

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>0.8.1</version>
</dependency>

这是我的导入声明:     import com.jayway.jsonpath.JsonPath

相关问题