我正在将2个jsons与JSR223 Assertion进行比较,我愿意从响应json中删除所有级别的ID:
{
"id" : 52906,
"name1" : "559812 Company name1",
"name2" : "559812 Company name2",
"country" : "DE",
"interests" : {
"id" : 848675,
"description" : false
},
"emails" : [ {
"id" : 904881,
"address" : "559812@gmail.com"
} ],
...
我正在使用以下Groovy代码:
def slurper2 = new JsonSlurper();
def jsonResponse = slurper2.parseText(prev.getResponseDataAsString());
jsonResponse.rows.findAll { it.remove("id") };
但它不起作用 - 请告知。
答案 0 :(得分:2)
我真的不明白你在哪里得到rows
位,因为我在你的回复中没有看到任何名为“行”的JSON Array。
如果您要删除所有“id”属性,可以使用以下方法:
def response = prev.getResponseDataAsString()
def responseWithoutIds = response.replaceAll("\"id\"[ ]*:[^,}\\]]*[,]?", "")
// do what you need with the modified response, i.e. store it into a JMeter Variable
vars.put("responseWithoutIds", responseWithoutIds)
演示:
参考文献:
答案 1 :(得分:0)
import groovy.json.*;
def s='''{
"id" : 52906,
"name1" : "559812 Company name1",
"name2" : "559812 Company name2",
"country" : "DE",
"interests" : {
"id" : 848675,
"description" : false
},
"emails" : [ {
"id" : 904881,
"address" : "559812@gmail.com"
} ]
}
'''
def removeAttr(node, attr){
if(node instanceof Map){
node.remove(attr)
node.each{ k,v-> removeAttr(v, attr) }
}else if(node instanceof List){
node.each{ i-> removeAttr(i, attr) }
}
}
def slurper2 = new JsonSlurper();
def jsonResponse = slurper2.parseText(s);
removeAttr(jsonResponse,"id")
println JsonOutput.prettyPrint(JsonOutput.toJson(jsonResponse))