Recursion groovy json中的递归闭包

Recursion groovy json中的递归闭包,recursion,groovy,Recursion,Groovy,我试图从一个键名为“label”的json中获取所有值,并希望将其存储在一个列表中。 我的问题是,标签键的位置不是固定的。有时它在父节点下,有时在子节点下,有时在子节点到子节点下。我们可以在groovy中使用递归闭包,但我不知道如何使用 Json:: [ { { "id": "2", "label": "NameWhatever" }, { "id": "123", "name": "Some Paren

我试图从一个键名为“label”的json中获取所有值,并希望将其存储在一个列表中。 我的问题是,标签键的位置不是固定的。有时它在父节点下,有时在子节点下,有时在子节点到子节点下。我们可以在groovy中使用递归闭包,但我不知道如何使用

Json::

[
  {
    {
        "id": "2",
        "label": "NameWhatever"
    },
    {
        "id": "123",
        "name": "Some Parent Element",
        "children": [{
                "id": "123123",
                "label": "NameWhatever"
            },
            {
                "id": "123123123",
                "name": "Element with Additional Children",
                "children": [{
                        "id": "123123123",
                        "label": "WhateverChildName"
                    },
                    {
                        "id": "12112",
                        "name": "Element with Additional Children",
                        "children": [{
                                "id": "123123123",
                                "label": "WhateverChildName"
                            },
                            {
                                "id": "12112",
                                "name": "Element with Additional Children",
                                "children": [{
                                        "id": "12318123",
                                        "label": "WhateverChildName"
                                    },
                                    {
                                        "id": "12112",
                                        "label": "NameToMap"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
基于

假设变量
json
包含来自问题的给定json输入,则打印:

[NameWhatever, NameWhatever, WhateverChildName, WhateverChildName, WhateverChildName, NameToMap]
[NameWhatever, NameWhatever, WhateverChildName, WhateverChildName, WhateverChildName, NameToMap]