接口测试,生成如下格式的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
}
}
网友评论