从SOAPUI中的json请求获取值

时间:2017-02-16 21:01:41

标签: json groovy soapui

我试图在模拟来自SOAPUI的响应时从json请求获取TxnType的值。我想根据TxnType的价值回复不同的回复。

{
    "Request": {
        "UserId": "user",
        "TxnType": "fetch"
    }
}

2 个答案:

答案 0 :(得分:1)

以下是使用修复json

获取请求值的groovy脚本
def json = """{
    "Request": {
        "UserId": "user",
        "TxnType": "fetch"
    }
}"""

def transactionType = new groovy.json.JsonSlurper().parseText(json).Request.TxnType
log.info "TxnType is : ${transactionType}"

您可以快速尝试 Demo

如果你想在模拟脚本中使用动态json,那么你可以在模拟脚本调度程序下面使用

def transactionType = new groovy.json.JsonSlurper().parseText(mockRequest.requestContent).Request.TxnType
log.info "TxnType is : ${transactionType}"

答案 1 :(得分:1)

//选择请求正文

def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

//选择TxnType值

def txnType = requestBody["TxnType"]

(或类似的东西)