英文:
Groovy Json parse with filter
问题
I'm beginner with groovy. Could you please help with parse all value by pattern from json?
So I need to get result with filter like "Name2-develop-FullPackage*" and exclude all other value. Ideally write these values into an array.
I tried code below, but I got blank [] result.
英文:
I'm beginner with groovy. Could you please help with parse all value by pattern from json?
String json = '''{
"_class" : "hudson.model.FreeStyleProject",
"builds" : [
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "#1500",
"number" : 1500
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name1-develop-(#1499)",
"number" : 1499
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name2-develop-FullPackage-(#1498)",
"number" : 1498
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name2-develop-FullPackage-(#1497)",
"number" : 1497
}
]
}'''
So I need to get result with filter like Name2-develop-FullPackage*
and exclude all other value. Ideally write these values into an array.
I tried code below, but I got blank [] result
def jsonSlurper = new JsonSlurper()
def obj = jsonSlurper.parseText(json)
def result = findAll{ obj.builds.displayName == "Name2-develop-FullPackage" }
println(result)
答案1
得分: 0
你的代码几乎正确:
String json = '''
{
"_class" : "hudson.model.FreeStyleProject",
"builds" : [
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "#1500",
"number" : 1500
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name1-develop-(#1499)",
"number" : 1499
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name2-develop-FullPackage-(#1498)",
"number" : 1498
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name2-develop-FullPackage-(#1497)",
"number" : 1497
}
]
}'''
def obj = new groovy.json.JsonSlurper().parseText(json)
def result = obj.builds.findResults{
it.displayName.startsWith('Name2-develop-FullPackage') ? [displayName: it.displayName, number: it.number] : null
}
assert result.size() == 2
assert result.toString() == '[[displayName:Name2-develop-FullPackage-(#1498), number:1498], [displayName:Name2-develop-FullPackage-(#1497), number:1497]]'
英文:
You got it almost right:
String json = '''{
"_class" : "hudson.model.FreeStyleProject",
"builds" : [
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "#1500",
"number" : 1500
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name1-develop-(#1499)",
"number" : 1499
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name2-develop-FullPackage-(#1498)",
"number" : 1498
},
{
"_class" : "hudson.model.FreeStyleBuild",
"displayName" : "Name2-develop-FullPackage-(#1497)",
"number" : 1497
}
]
}'''
def obj = new groovy.json.JsonSlurper().parseText json
def result = obj.builds.findResults{
it.displayName.startsWith( 'Name2-develop-FullPackage' ) ? [ displayName:it.displayName, number:it.number ] : null
}
assert result.size() == 2
assert result.toString() == '[[displayName:Name2-develop-FullPackage-(#1498), number:1498], [displayName:Name2-develop-FullPackage-(#1497), number:1497]]'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论