Groovy JSON解析与过滤

huangapple go评论114阅读模式
英文:

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]]'

huangapple
  • 本文由 发表于 2023年3月9日 17:13:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定