简体   繁体   English

如何使用Groovy从soapui Json Response构造JsonPath?

[英]How to construct the JsonPath from soapui Json Response using groovy?

I have an soapui response like below and i tried to parse the same and print all the elements(From leaf node) in the json response. 我有如下所示的soapui响应,我试图解析相同的内容并打印json响应中的所有元素(来自叶节点)。

Sample Json : 样本Json:

{
    "BookID": 7982,
    "author": {
        "authorname": "roboin"

    },
    "authorid": "X-1-23",
    "BookDetails": [{
        "Price": "100",
        "Location": "Paris"
    }],
    "authordob": "1980-11-10",

    "Adverts": {
        "online": true

    }
}

Use of below groovy script is to print all the elements in the response.The below code goes to each and every element in the Json response and print like below Expected Result, 下面的groovy脚本的使用是打印响应中的所有元素。下面的代码转到Json响应中的每个元素,并按以下“预期结果”进行打印,

Expected Result: Print all the element(leaf node) jsonpath and values 预期结果: 打印所有元素(叶节点)jsonpath和值

$.['author']['authorname'] : roboin $。['author'] ['authorname']:机器人

$.['BookDetails'][0]['Price']:100 $ [ 'BookDetails'] [0] [ '价格']:100

Current Result : Prints all the elements and values 当前结果: 打印所有元素和值

authorname : roboin 作者名称:roboin

Price:100 价格:100

import groovy.json.*

 //Get the test case response  from context and parse it
def contextResponse = messageExchange.getResponseContent().toString()
//log.info(contextResponse)

def parseResponse = new JsonSlurper().parseText(contextResponse)
//log.info(parseResponse)

def parseMap(map) {
    map.each {
        if (it.value instanceof Map) {
            parseMap(it.value)
        } else if (it.value instanceof List) {
            log.info(it.key + ": ")
            parseArray(it.value)
        } else {
            log.info(it.key + ": " + it.value)
        }
    }
}

def parseArray(array) {
    array.each {
        if (it instanceof Map) {
            parseMap(it)
        } else if (it instanceof List) {
            parseArray(it)
        } else {
            log.info("arrayValue: $it");
        }
    }
}

parseMap(parseResponse)

I tried some research about this and found few json path selector in online and that can't be used inside my soapui application.i want to iterate and print all the elements json path and their values. 我对此进行了一些研究,发现在线没有几个json路径选择器,并且无法在我的soapui应用程序中使用。我想迭代并打印所有元素json路径及其值。

Currently the above code iterate and prints only the element name and values. 当前,上面的代码迭代并仅打印元素名称和值。

def j=new groovy.json.JsonSlurper().parseText('''{
    "BookID": 7982,
    "author": {
        "authorname": "roboin"

    },
    "authorid": "X-1-23",
    "BookDetails": [{
        "Price": "100",
        "Location": "Paris"
    }],
    "authordob": "1980-11-10",

    "Adverts": {
        "online": true

    }
}''')

void printJsonPaths(o, path='$'){
    if(o instanceof Map){
        o.each{ k,v-> printJsonPaths(v, path+"['${k}']") }
    }else if(o instanceof List){
        o.eachWithIndex{ v,i-> printJsonPaths(v, path+"[${i}]") }
    }else{
        println("${path}: ${o}")
    }
}
printJsonPaths(j)

output 产量

$['BookID']: 7982
$['author']['authorname']: roboin
$['authorid']: X-1-23
$['BookDetails'][0]['Price']: 100
$['BookDetails'][0]['Location']: Paris
$['authordob']: 1980-11-10
$['Adverts']['online']: true

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

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