【问题标题】:Groovy : how do i search json with key's value and find its children in groovyGroovy:我如何用键的值搜索 json 并在 groovy 中找到它的孩子
【发布时间】:2014-05-14 12:22:38
【问题描述】:
[
{
    "createTime": "2014-05-12 04:51:46.513343",
    "powered": false,
    "description": "s",
    "current": false,
    "children": [
        {
            "createTime": "2014-05-13 03:50:43.050442",
            "powered": false,
            "description": "Snapshot description",
            "current": false,
            "children": [
                {
                    "createTime": "2014-05-13 03:57:08.209319",
                    "powered": false,
                    "description": "s",
                    "current": false,
                    "children": [
                        {
                            "createTime": "2014-05-13 04:27:00.646064",
                            "powered": false,
                            "description": "s",
                            "current": false,
                            "label": "snap3"
                        },
                        {
                            "createTime": "2014-05-13 21:00:16.374178",
                            "powered": false,
                            "description": "sd",
                            "current": false,
                            "label": "sddsds"
                        }
                    ],
                    "label": "snap2"
                }
            ],
            "label": "snapshot-name5"
        },
        {
            "createTime": "2014-05-14 00:49:33.415858",
            "powered": false,
            "description": "a",
            "current": false,
            "children": [
                {
                    "createTime": "2014-05-14 02:35:10.076829",
                    "powered": false,
                    "description": "sdfsdfsdf",
                    "current": true,
                    "label": "ssfsdf"
                }
            ],
            "label": "assa"
        }
    ],
    "label": "snap1"
}
]

在这里,我将标签“snap2”作为输入,我需要将其子标签(snap3、sddsds)作为输出。我如何在 groovy 中做到这一点?我搜索了很多东西,但我无法找到解决方案..

如果我能得到你们任何人的建议,那就太好了。

【问题讨论】:

    标签: json groovy


    【解决方案1】:

    假设您的 json 在字符串 jsonTxt 中,您可以这样做:

    def json = ​new groovy.json.JsonSlurper().parseText( jsonTxt )
    
    def recursiveChildrenScan( map, key, value ) {
        if( !map ) { null }
        else if( map[ key ] == value ) { map.children }
        else { map.children.findResult { recursiveChildrenScan( it, key, value ) } }
    }
    
    println json.findResult { recursiveChildrenScan( it, 'label', 'snap2' ) }​?.label​
    

    【讨论】:

    【解决方案2】:
    find_recursive(new groovy.json.JsonSlurper().parseText(your_json_here))
    
    def find_recursive(a) {
        a.collect {(it.label == "snap2") ? it.children.collect {it.label} : find_recursive(it.children) }.flatten()
    }
    

    【讨论】:

    • 感谢 kunal,抽出宝贵时间。我没有尝试过这段代码。我希望有同样问题的人可以尝试这个并提供反馈。
    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多