奇怪的是,您的响应而不是纯Json对象包含jsonCallback()
包裹您的Json的字符串。
因此,假设您的响应包含此文本,您必须在解析Json对象之前将其删除,一旦将其删除,您就可以使用 解析它JsonSlurper
并使用正确的路径访问您的preToken
元素:
import groovy.json.JsonSlurper
def response = '''jsonCallback({"validationErrors":null,
"response":{"type":"PreTokenResponse",
"preToken":"43a81ef8-693d-4e3d-ad83-f8ed76f39756",
"errors":null},
"exception":null});'''
// remove the unnecesary data
response = response - 'jsonCallback('
response = response - ');'
// now you've a correct json
log.info response
// parse the json
def json = new JsonSlurper().parseText(response)
// access the desired value
log.info json.response.preToken
assert '43a81ef8-693d-4e3d-ad83-f8ed76f39756' == json.response.preToken
我将您的响应用作字符串只是为了在 SOAPUI 中为您提供一个工作示例,它必须相同,但从您的testStepresponse
的属性中获取:
def response = testRunner.testCase.getTestStepByName("xxx").getPropertyValue("response")