我有这个json:
{
"books": [
{
"bookId": "1",
"bookName": "name1",
"bookYear": "2010"
},
{
"bookId": "2",
"bookName": "name2",
"bookYear": "2010"
}
]
}
,我想删除包含"bookId"; "1">的列表结果应该是:
{
"books": [
{
"bookId": "2",
"bookName": "name2",
"bookYear": "2010"
}
]
}
我试过这个代码:
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
JsonSlurper slurper = new JsonSlurper()
String jsonString = vars.get("jsonOutput")
def parsedJson = slurper.parseText(jsonString)
log.info("Json before: " + jsonString)
int num = parsedJson.books.size()
for(int i=0;i<parsedJson.books.size();i++){
parsedJson.books[i].values().removeAll(value -> value.equals("1"))
}
def json = JsonOutput.toJson(parsedJson)
log.info("Json after: " + json.toString())
但是它只删除了" bookd "从列表:
Json before: {"books":[{"bookId":"1","bookName":"name1","bookYear":"2010"},{"bookId":"2","bookName":"name2","bookYear":"2010"}]}
Json after: {"books":[{"bookName":"name1","bookYear":"2010"},{"bookId":"2","bookName":"name2","bookYear":"2010"}]}
请你帮我一下好吗?您可以这样筛选图书列表:
def parsedJson = slurper.parseText(jsonString)
parsedJson.books = parsedJson.books.findAll { it.bookId != '1' }
def json = JsonOutput.toJson(parsedJson)