我想验证'questionId'是一个数字。如何使用groovy脚本验证它?

时间:2017-11-27 07:59:26

标签: testing groovy soapui web-api-testing jsonresponse

如下所示,json对象的一部分需要提取questionId并检查它是否为数字:

{
    "rootProductQuestionId": "17",
    "questionAnswers": [ 
        {
            "question": "",
            "questionId": "17",
            "answer": ""
        }
    ],

    "productId": "8",
    "notes": ""
}

1 个答案:

答案 0 :(得分:2)

您可以选择isNumber()检查,例如:

import groovy.json.jsonSlurper
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText('{"rootProductQuestionId":"17","questionAnswers":[{"question":"","questionId":"17","answer":""}],"productId":"8","notes":""}')
return json.questionAnswers.questionId[0].isNumber()