0

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

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

4

2 回答 2

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
于 2016-02-22T08:18:27.630 回答
0

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

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

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

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

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

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

于 2012-06-18T22:37:42.583 回答