仅从Groovy中深度嵌套的Json响应中检索密钥

时间:2020-06-25 03:03:18

标签: json dictionary groovy jmeter

我有一个深层嵌套的json,现在我可以检索前三个级别的键,现在我有了第三个级别,并像这样访问(checks.payments.paymentToItems))变量,变量的值是像下面这样打印

[[[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 06:51:11.084276+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 06:51:11.084276+00:00]]], [[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 06:57:34.090822+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 06:57:34.090822+00:00]]], [[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 07:02:24.829866+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 07:02:24.829866+00:00]]], [[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 07:04:23.192466+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 07:04:23.192466+00:00]]], [[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 07:08:23.481073+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 07:08:23.481073+00:00]]], [[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 09:51:38.040312+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 09:51:38.040312+00:00]]], [[[id:check, declaredAmount:13.74, paidAmount:13.74, timestamp:2020-06-24 09:53:59.884796+00:00], [id:tip, declaredAmount:2.15, paidAmount:2.15, timestamp:2020-06-24 09:53:59.884796+00:00]]]]

现在我只想检索键并添加到列表以验证每个字段。 我尝试了以下方法,因为地图以四个方括号[[[[但仅获取空值 有人可以请问一下。

        log.info("-----------------------payments $paymentToItems");
        def lstKeys=[]
        //taxMap.each { entry -> lstKeys.add( $entry.key") }
        
        for (entry in paymentToItems) {
             
             def map =entry
             for(entry2 in map){
             log.info("-----------------------paymentkeys $lstKeys");
                def finalMap =entry2 
                for(entry3 in finalMap){
                    def lasMap = entry3
                //for(entry4 in lasMap)
                    def key=entry3.key 
                        lstKeys.add(key)
                    
                }
                
             }
             
            log.info("-----------------------paymentkeys $lstKeys");
        } ```

$lstKeys keys print null values

2020-06-25 01:58:11,356 INFO o.a.j.a.J.JSR223 Assertion: -----------------------paymentkeys [null, null]
2020-06-25 01:58:11,357 INFO o.a.j.a.J.JSR223 Assertion: -----------------------paymentkeys [null, null, null, null]
2020-06-25 01:58:11,357 INFO o.a.j.a.J.JSR223 Assertion: -----------------------paymentkeys [null, null, null, null, null, null]
2020-06-25 01:58:11,357 INFO o.a.j.a.J.JSR223 Assertion: -----------------------paymentkeys [null, null, null, null, null, null, null, null]
2020-06-25 01:58:11,357 INFO o.a.j.a.J.JSR223 Assertion: -----------------------paymentkeys [null, null, null, null, null, null, null, null, null, null]
2020-06-25 01:58:11,357 INFO o.a.j.a.J.JSR223 Assertion: -----------------------paymentkeys [null, null, null, null, null, null, null, null, null, null, null, null]

1 个答案:

答案 0 :(得分:0)

如果您不确定要与哪种野兽战斗,请携带 数据转换为结构化的分层形式:

def data = [ /*1*/
    [ /*2*/ [ /*3*/
        [id:'check', declaredAmount:13.74, paidAmount:13.74, timestamp:'2020-06-24 06:51:11.084276+00:00'],
        [id:'tip',   declaredAmount:2.15,  paidAmount:2.15,  timestamp:'2020-06-24 06:51:11.084276+00:00']
    ]], 
    [[
        [id:'check', declaredAmount:13.74, paidAmount:13.74, timestamp:'2020-06-24 06:57:34.090822+00:00'],
        [id:'tip',   declaredAmount:2.15,  paidAmount:2.15,  timestamp:'2020-06-24 06:57:34.090822+00:00']
    ]] 
    // ...
]

所以您的数据是

  1. 列表
  2. 仅列出一个列表
  3. 地图列表

因此,这里最大的“问题”是无用的#2。你可以摆脱 通过例如.first()导航时。例如

data.each{
    it.first().each{
        println it.id
    }
}

或者您应该不要小心,以在 第一名。确保您在上游正确构建数据 而不是像从那以后那样对抗不便 之后。

注意:来自groovy的常规.toString()仅用于快速 阅读,但与他人分享时无用。鉴于你只有简单 数据,请使用.inspect()获取其他人可以使用的东西。例如。 println data.inspect()