背景:测试接口,数据为json串,如下所示:
{
"head":{
"a":"a1",
"b":"b1",
"c":"c1",
"d":"d1"
},
"body":{
"o":"o1",
"p":"p1",
"q":"q1"
}
}
groovy脚本如下:
//导入加密jar包
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
head = [
a:'a1',
b:'b1',
c:'c1',
d:'d1'
]
body = [
o:'o1',
p:'p1',
q:'q1'
]
//Tools.filteMap(map) 过滤body里面的空值项
body = Tools.filteMap(body)
//生成json串请求
def requestJson = new JsonBuilder()
requestJson{
head head
body body
}
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
}
}
网友评论