下面的列表包含id
的
List id_wanted = ['3894586', '2786438236', '895673985']
鉴于上面的列表,如何从下面的JSON中删除与列表上方的ID相匹配的元素?
JSON:
{
"animals": [
{
"name": "lion",
"countries": [
{
"name": "kenya",
"facts": [
{
"features": [
"young male"
],
"age": "2y",
"id": "2837492"
}
]
},
{
"name": "tanzania",
"facts": [
{
"features": [
"cub"
],
"age": "0y",
"id": "3894586"
}
]
},
{
"name": "south africa",
"facts": [
{
"features": [
"adult lioness"
],
"age": "10y",
"id": "495684576"
},
{
"features": [
"young female"
],
"age": "4y",
"id": "2786438236"
}
]
}
]
},
{
"name": "giraffe",
"countries": [
{
"name": "zambia",
"facts": [
{
"features": [
"ex captivity"
],
"age": "20y",
"id": "343453509"
}
]
},
{
"name": "kenya",
"facts": [
{
"features": [
"male"
],
"age": "17y",
"id": "85604586"
}
]
},
{
"name": "uganda",
"facts": [
{
"features": [
"young female"
],
"age": "4y",
"id": "895673985"
},
{
"features": [
"none"
],
"age": "11y",
"id": "39860394758936764"
}
]
}
]
}
]
}
例如,由于id
与列表id_wanted
匹配,因此将从上面的JSON中删除以下块
{
"features": [
"young female"
],
"age": "4y",
"id": "2786438236"
}
答案 0 :(得分:0)
假设您的json是变量inputJson
中的字符串,那么从原始文件中过滤掉那些值来构建新的Json文档可能会更容易:
import groovy.json.*
def json = new JsonSlurper().parseText(inputJson)
List id_wanted = ['3894586', '2786438236', '895673985']
def result = new JsonBuilder([
animals: json.animals.collect {[
name: "$it.name",
countries: it.countries.collect { [
name: "$it.name",
facts: it.facts.findAll { !(it.id in id_wanted) }
]}
]}
]).toString()
答案 1 :(得分:0)
您可以解析//
// Call classifier on data
//
let pyStcriptPath = "\(Bundle.main.bundlePath)/Classification/****Classify.py"
//
// The fonction I'm calling here, 'shell', is the one defined just above in my question
//
let pyResult = shell("python", pyStcriptPath, "\(featureVector)", "\(signalType)")
json并使用方便的original
扩展运算符就地修改结果数据结构:
*.
输出(已删除def json = slurper.parseText(original)
json.animals*.countries*.facts*.each { facts ->
facts.removeAll { fact -> fact.id in id_wanted }
}
def filtered = new JsonBuilder(json).toPrettyString()
println(filtered)
中的事实):
id_wanted