简体   繁体   English

SOAPUI-使用Groovy脚本解析不同的JSON Response对象以获取相同的子节点值

[英]SOAPUI - Parse different JSON Response objects to fetch the same child node values using Groovy Scripting

I'm automating REST API's using SOAPUI. 我正在使用SOAPUI自动化REST API。 I've 2 different resources and below are the Response JSON formats. 我有2种不同的资源,下面是Response JSON格式。 The root element varies but the status block remains same for both the response jsons. 根元素有所不同,但两个响应json的状态块均相同。 After the Rest API POST call, I've a Groovy script in place to validate the response (if "code" == "00") 在Rest API POST调用之后,我准备了一个Groovy脚本来验证响应(如果“ code” ==“ 00”)

Goal: Using Groovy Script, I've to Parse through the JSON and retreive the "code" node value irrespective of the root element. 目标:使用Groovy脚本,我必须解析JSON并获取“代码”节点值,而与根元素无关。

Response(JSON) Formats : 响应(JSON)格式

{
    "resouurce_1_response":
    {
        "status":
        {
            "code": "00"
        }
    }
}

{
    "resource_2_response":
    {
        "status":
        {
            "code": "00"
        }
    }
}

One quick way of doing it would be: 一种快速的方法是:

def json1 = '{ "resouurce_1_response": { "status": { "code": "00" } } }'
def json2 = '{ "resource_2_response": { "status": { "code": "00" } } }'

import groovy.json.*

def slurper = new JsonSlurper()

assert slurper.parseText(json1).find().value.status.code == '00'
assert slurper.parseText(json2).find().value.status.code == '00'

Of course, if your actual Json is more complex than you show, you might need to do something different (recursively walk the map?) 当然,如果实际的Json比显示的要复杂,您可能需要做一些不同的事情(递归地走地图?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM