首页 > 解决方案 > 从 JSON 有效负载中删除特定节点,保持子节点完整

问题描述

我是 groovy 的新手,需要帮助。我需要在下面的有效负载中删除一个段(segment = 'ToBeRemoved'),并且需要在 groovy 中执行此操作。通过谷歌尝试了一些代码,但进展不大,任何人都可以帮忙。

输入:

{
    "Root": {
        "Somedata": true,
        "Myoperation": {
            "Row": [
                {
                    "Action": "CreateOrUpdate",
                    "Object": {
                        "a": true,
                        "b": "test",
                        "c": "test"
                    }
                }, {
                    "Action": "CreateOrUpdate",
                    "Object": {
                        "Action": "CreateOrUpdate",
                        "Object": {
                            "e": true,
                            "f": "test",
                            "g": "test"
                        },
                        "O1": {
                            "ToBeRemoved": [
                                {
                                    "Key": 4
                                }, {
                                    "Key": 4
                                }, {
                                    "Key": 4
                                }, {
                                    "Key": 4
                                }, {
                                    "Key": 4
                                }
                            ]
                        },
                        "O2": {
                            "ToBeRemoved": [
                                {
                                    "Key": "7e6184bb94576c9d"
                                }, {
                                    "Key": "ca6da5d833b6866f"
                                }, {
                                    "Key": "ca6da5d833b6866f"
                                }
                            ]
                        },
                        "O3": {
                            "ToBeRemoved": [
                                {
                                    "Key": 4.00210590000900225
                                }, {
                                    "Key": 4.00210590000900225
                                }, {
                                    "Key": 4.00210610200900225
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

输出

{
    "Root": {
        "Somedata": true,
        "Myoperation": {
            "Row": [
                {
                    "Action": "CreateOrUpdate",
                    "Object": {
                        "a": true,
                        "b": "test",
                        "c": "test"
                    }
                }, {
                    "Action": "CreateOrUpdate",
                    "Object": {
                        "Action": "CreateOrUpdate",
                        "Object": {
                            "e": true,
                            "f": "test",
                            "g": "test"
                        },
                        "O1": [
                            {
                                "Key": 4
                            }, {
                                "Key": 4
                            }, {
                                "Key": 4
                            }, {
                                "Key": 4
                            }, {
                                "Key": 4
                            }
                        ],
                        "O2": [
                            {
                                "Key": "7e6184bb94576c9d"
                            }, {
                                "Key": "ca6da5d833b6866f"
                            }, {
                                "Key": "ca6da5d833b6866f"
                            }
                        ],
                        "O3": [
                            {
                                "Key": 4.00210590000900225
                            }, {
                                "Key": 4.00210590000900225
                            }, {
                                "Key": 4.00210610200900225
                            }
                        ]
                    }
                }
            ]
        }
    }
}

我试图解析输入的代码

JsonSlurper slurper = new JsonSlurper() Map parsedJson = slurper.parseText(body)

字符串段 = parsedJson.get("MultiOperation").get("Operations").get("Row")。

标签: groovy

解决方案


推荐阅读