我如何将 json 对象的值设置为 soapUI 中的请求

How could I set value of json object as a Request in soapUI

我有一个 json 请求是这样的:

{
    "scopeId":"",
    "scopeType":"",
    "userId":"",
    "fieldToBeEncryptedList":[
    {
    "srNo":"",
    "fieldName":"",
    "value":"",
    "echoField":""
    }
]
}

现在我想要的是动态设置每个键的值,这是我从其他测试步骤的响应中获得的。我如何使用 groovy 脚本来做到这一点。

提前致谢。

您可能需要这样做:

​import groovy.json.JsonBuilder
import groovy.json.JsonSlurper  
def jsonStr = '{"scopeId":" ","scopeType":" ","userId":" ","fieldToBeEncryptedList":[{"srNo":" ","fieldName":" ","value":" ","echoField":" "}]}'
def slurp = new JsonSlurper().parseText(jsonStr)  
def builder = new JsonBuilder(slurp)  
builder.content.scopeId = 'val1'  
builder.content.fieldToBeEncryptedList[0].srNo = 'val2'
println builder.toPrettyString()​

输出:

{
    "fieldToBeEncryptedList": [
        {
            "echoField": " ",
            "fieldName": " ",
            "srNo": "val2",
            "value": " "
        }
    ],
    "scopeId": "val1",
    "scopeType": " ",
    "userId": " "
}

你的问题中缺少一些细节,但我想你有两个 REST TestSteps,假设第一个叫做 REST Test Request 而第二个 REST Test Request2 你可以设置响应并使用 groovy TestStep:

中的以下 groovy 脚本获取 testSteps 的请求值
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

// request
def request = '''{"scopeId":"",
    "scopeType":"",
    "userId":"",
    "fieldToBeEncryptedList":[{"srNo":"","fieldName":"","value":"","echoField":""}]
}'''
// parse the request
def jsonReq = new JsonSlurper().parseText(request);

// get the response where you've the values you want to get
// using the name of your test step
def response = context.expand('${REST Test Request#Response}')
// parse response
def jsonResp = new JsonSlurper().parseText(response)

// get the values from your first test response 
// and set it in the request of the second test
jsonReq.scopeId = jsonResp.someField
jsonReq.scopeType = jsonResp.someObject.someField
// ... 

// parse json to string in order to save it as a property
def jsonReqAsString = JsonOutput.toJson(jsonReq)
// save as request for the next testStep
def restRequest = testRunner.testCase.getTestStepByName('REST Test Request2');
restRequest.setPropertyValue('Request',jsonReqAsString);

此脚本获取您的 json 并用第一个 testStep 响应中的值填充它,然后将此 json 设置为第二个 testStep 的请求.

希望这对您有所帮助,