如何解析GROOVY中任何级别的JSON数据值

时间:2016-10-13 15:30:18

标签: json groovy soapui

以下是我与Json Surplur的Parsed JSON。我已经要求SOAPUI脚本进行操作了

 {buildInfo={application=RepCatalog, buildDate=Thu Oct 13 17:01:48 IST 2016, version=1.0.0}, data={Reps=[{cascadeCount=0, catalogRep={RepId=48961, RepType=REPORT, initialCreation=10/13/2016 20:39:11, lastAccessed=10/13/2016 20:39:11, lastModified=10/13/2016 20:39:11, parentRep={RepId=48962, RepType=REPORT, initialCreation=10/13/2016 20:39:14, lastAccessed=10/13/2016 20:39:14, lastModified=10/13/2016 20:39:14, status=OPEN, title=REPORT1476371359504}, rights=[{availability=PUBLIC, isDefault=true}], status=OPEN, title=REPORT1476371357505, userTags=[PRIVATE1476371349835]}, status=success}]}, status=success, summary={apiName=Integration Service, partialRepSucceeded=0, totalRepFailed=0, totalRepProccessed=1, totalRepSucceeded=1}, time=6674}

以下是未解析的JSON

{
   "summary":    {
      "apiName": "Integration Service",
      "totalRepProccessed": 1,
      "totalRepFailed": 0,
      "totalRepSucceeded": 1,
      "partialRepSucceeded": 0
   },
   "buildInfo":    {
      "application": "RepCatalog",
      "version": "1.0.0",
      "buildDate": "Thu Oct 13 17:01:48 IST 2016"
   },
   "status": "success",
   "data": {"Reps": [   {
      "status": "success",
      "catalogRep":       {
         "RepId": 48961,
         "RepType": "REPORT",
         "title": "REPORT1476371357505",
         "rights": [         {
            "availability": "PUBLIC",
            "isDefault": true
         }],
         "initialCreation": "10/13/2016 20:39:11",
         "lastModified": "10/13/2016 20:39:11",
         "lastAccessed": "10/13/2016 20:39:11",
         "status": "OPEN",
         "parentRep":          {
            "RepId": 48962,
            "RepType": "REPORT",
            "title": "REPORT1476371359504",
            "status": "OPEN"
         },
         "userTags": ["PRIVATE1476371349835"]
      },
      "cascadeCount": 0
   }]},
   "time": 6674
}

我想解析它以获取Groovy SOAPUI中的All RepId值

2 个答案:

答案 0 :(得分:1)

将输入作为名为json的字符串变量,以下脚本:

def extractRepIds (def tree, def ids = []) {
    switch (tree) {
        case Map:
            tree.each { k, v ->
                if (k == "RepId") { ids << v }
                extractRepIds(v, ids)
            }
            return ids
        case Collection:
            tree.each { e -> extractRepIds(e, ids) }
            return ids
        default :
            return ids
    }
}

def extractRepIdsFromJson(def jsonString) {
    def tree = new JsonSlurper().parseText(jsonString)
    extractRepIds(tree)
}

println extractRepIdsFromJson(json)

产生以下结果:

[48961, 48962]

替代解决方案

使用extractRepIds方法可以更清晰地编写inject()方法:

def extractRepIds (def tree) {
    switch (tree) {
        case Map:
            return tree.inject([]) { list, k, v ->
                list + (k == "RepId" ? [v] : extractRepIds(v))
            }
        case Collection:
            return tree.inject([]) { list, e ->
                list + extractRepIds(e)
            }
        default :
            return []
    }
}

如果其他所有相同,则产生相同的结果。

答案 1 :(得分:0)

如果要查找文件中的所有匹配项,可以使用正则表达式。在这种情况下它可能很有用。

def pattern = ~'\"RepId\":\\s(\\d+)' //"RepId": 48961
def path = "/tmp/data.json"
def data = new File(path).text

def repIds = []
pattern.matcher(data).findAll{fullMatch,repId ->
    repIds << repId
}