簡體   English   中英

Groovy從地圖上刪除

[英]Groovy remove from map

下面的列表包含id的集合

List id_wanted = ['3894586', '2786438236', '895673985']

給定上面的列表,如何從下面的JSON中刪除與列表上方的ID相匹配的元素?

JSON:

{
    "animals": [
        {
            "name": "lion",
            "countries": [
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "young male"
                            ],
                            "age": "2y",
                            "id": "2837492"
                        }
                    ]
                },
                {
                    "name": "tanzania",
                    "facts": [
                        {
                            "features": [
                                "cub"
                            ],
                            "age": "0y",
                            "id": "3894586"
                        }
                    ]
                },
                {
                    "name": "south africa",
                    "facts": [
                        {
                            "features": [
                                "adult lioness"
                            ],
                            "age": "10y",
                            "id": "495684576"
                        },
                        {
                            "features": [
                                "young female"
                            ],
                            "age": "4y",
                            "id": "2786438236"
                        }
                    ]
                }
            ]
        },
        {
            "name": "giraffe",
            "countries": [
                {
                    "name": "zambia",
                    "facts": [
                        {
                            "features": [
                                "ex captivity"
                            ],
                            "age": "20y",
                            "id": "343453509"
                        }
                    ]
                },
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "male"
                            ],
                            "age": "17y",
                            "id": "85604586"
                        }
                    ]
                },
                {
                    "name": "uganda",
                    "facts": [
                        {
                            "features": [
                                "young female"
                            ],
                            "age": "4y",
                            "id": "895673985"
                        },
                        {
                            "features": [
                                "none"
                            ],
                            "age": "11y",
                            "id": "39860394758936764"
                        }
                    ]
                }
            ]
        }
    ]
}

例如,下面的塊將從上面的JSON中刪除,因為id與列表id_wanted匹配

                    {
                        "features": [
                            "young female"
                        ],
                        "age": "4y",
                        "id": "2786438236"
                    }

假設您的json是變量inputJson的String,那么從原始文件中過濾掉那些值來構建新的Json文檔可能會更容易:

import groovy.json.*

def json = new JsonSlurper().parseText(inputJson)

List id_wanted = ['3894586', '2786438236', '895673985']

def result = new JsonBuilder([
    animals: json.animals.collect {[
        name: "$it.name",
        countries: it.countries.collect { [
            name: "$it.name",
            facts: it.facts.findAll { !(it.id in id_wanted) }
        ]}
    ]}
]).toString()

您可以使用方便的*.解析original json並就地修改結果數據結構*. 點差運算符:

def json = slurper.parseText(original)
json.animals*.countries*.facts*.each { facts ->
    facts.removeAll { fact -> fact.id in id_wanted }
}
def filtered = new JsonBuilder(json).toPrettyString()
println(filtered)

輸出(刪除了id_wanted事實):

{
    "animals": [
        {
            "name": "lion",
            "countries": [
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "young male"
                            ],
                            "age": "2y",
                            "id": "2837492"
                        }
                    ]
                },
                {
                    "name": "tanzania",
                    "facts": [

                    ]
                },
                {
                    "name": "south africa",
                    "facts": [
                        {
                            "features": [
                                "adult lioness"
                            ],
                            "age": "10y",
                            "id": "495684576"
                        }
                    ]
                }
            ]
        },
        {
            "name": "giraffe",
            "countries": [
                {
                    "name": "zambia",
                    "facts": [
                        {
                            "features": [
                                "ex captivity"
                            ],
                            "age": "20y",
                            "id": "343453509"
                        }
                    ]
                },
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "male"
                            ],
                            "age": "17y",
                            "id": "85604586"
                        }
                    ]
                },
                {
                    "name": "uganda",
                    "facts": [
                        {
                            "features": [
                                "none"
                            ],
                            "age": "11y",
                            "id": "39860394758936764"
                        }
                    ]
                }
            ]
        }
    ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM