美文网首页接口测试
接口测试_soapui工具_groovy语言生成json请求串含

接口测试_soapui工具_groovy语言生成json请求串含

作者: freedom_smile | 来源:发表于2019-07-02 17:57 被阅读0次

接口测试,生成如下格式的json串

{
    "requestHead":{
        "a":"a1",
        "b":"b1",
        "c":"c1"
    },
    "o":"o1",
    "p":"p1",
    "q":"q1",
    "l":[
        {
            "r":"r1",
            "s":"s1"
        },
        {
            "t":"t1",
            "u":"u1"
        }
    ]
}

groovy代码如下:

//导入加密jar包
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper

requestHead = [
    'a':"a1",
    'b':"b1",
    'c':"c1"
]

requestAll = [
    requestHead:requestHead,
    'o':"o1",
    'p':"p1",
    'q':"q1",
    'l':[
          {
            "r":"r1",
            "s":"s1"
          },
          {
            "t":"t1",
            "u":"u1"
         }
    ]
]

//Tools.filteMap(map)  过滤空值项
requestFilter = Tools.filteMap(requestAll)

//生成json请求串
def requestJson = JsonOutput.toJson(requestFilter)
log.info JsonOutput.prettyPrint(requestJson.toString())

Tools.filteMap(map)编码如下:

class Tools {
 //过滤map里面的空值项 args传需要被过滤的map
    static def  filteMap(def args) {
        def filter_map = [:]
        args.each{
            if(it.value != ""){
                filter_map.put(it.key,it.value)
            }
        }
        return filter_map
    }
}

相关文章

网友评论

    本文标题:接口测试_soapui工具_groovy语言生成json请求串含

    本文链接:https://www.haomeiwen.com/subject/xepthctx.html