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

I want to verify that the 'questionId' is a number. How to verify it using groovy script?

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

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

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

您可以选择 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()