我如何限制Java中JSON的深度? (删除不再处于“深度范围”的条目;

发布于 2025-01-19 07:31:04 字数 2127 浏览 0 评论 0原文

目前我有一个基本上可以具有“无限”深度的 JSON。 我是否有可能限制深度并只得到 JSON 的前几个级别?也许甚至有像 Jackson 或 GS​​ON 这样的库?

当前的 JSON 可能如下所示:

    "id": "77ec211f-b6d1-46fb-afe8-c799e65db04c",
    "name": "Test",
    "type": "TOP",
    "children": [
        {
            "id": "c2ab68a3-dfb2-4002-ae07-826f30d169e3",
            "name": "Austin Fadel",
            "type": "MID",
            "children": []
        },
        {
            "id": "cafe8bf0-74a6-476f-99c3-4db584ec47fe",
            "name": "Spring Keebler",
            "type": "MID",
            "children": []
        },
        {
            "id": "53ff0d5f-ee7f-4648-a718-c9c3acbf44f4",
            "name": "Jinny Von I",
            "type": "MID",
            "children": [
                {
                    "id": "092c279a-baaa-4c1a-9656-7ecd5d2f3c97",
                    "name": "Max Power",
                    "type": "LOW",
                    "children": [
                        {
                            "id": "0925279a-baaa-4c1a-9656-7ecd5d2f3c97",
                            "name": "Max Lower",
                            "type": "LOW",
                            "children": []
                        }
                    ]
                }
            ]
        }
        {
            "id": "cd3bbcab-6ef4-459e-8d43-74dbd73c12c5",
            "name": "Jayson Feest",
            "type": "MID",
            "children": [
                {
                    "id": "092c279a-baaa-4c1a-9656-7ecd5d2f3c97",
                    "name": "Max Power",
                    "type": "LOW",
                    "children": [
                        {
                            "id": "0925279a-baaa-4c1a-9656-7ecd5d2f3c97",
                            "name": "Max Lower",
                            "type": "LOW",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "id": "2fb8e01c-ffc6-4535-8241-ad057946961a",
            "name": "Gladys Jast",
            "type": "MID",
            "children": []
        }
    ]
}

谢谢您的帮助

Currently I have a JSON that can basically have an "infinite" depth.
Is there any chance I can limit the depth and only get like the first couple levels of the JSON? Maybe even with a Library like Jackson or GSON?

Current JSON could look as following:

    "id": "77ec211f-b6d1-46fb-afe8-c799e65db04c",
    "name": "Test",
    "type": "TOP",
    "children": [
        {
            "id": "c2ab68a3-dfb2-4002-ae07-826f30d169e3",
            "name": "Austin Fadel",
            "type": "MID",
            "children": []
        },
        {
            "id": "cafe8bf0-74a6-476f-99c3-4db584ec47fe",
            "name": "Spring Keebler",
            "type": "MID",
            "children": []
        },
        {
            "id": "53ff0d5f-ee7f-4648-a718-c9c3acbf44f4",
            "name": "Jinny Von I",
            "type": "MID",
            "children": [
                {
                    "id": "092c279a-baaa-4c1a-9656-7ecd5d2f3c97",
                    "name": "Max Power",
                    "type": "LOW",
                    "children": [
                        {
                            "id": "0925279a-baaa-4c1a-9656-7ecd5d2f3c97",
                            "name": "Max Lower",
                            "type": "LOW",
                            "children": []
                        }
                    ]
                }
            ]
        }
        {
            "id": "cd3bbcab-6ef4-459e-8d43-74dbd73c12c5",
            "name": "Jayson Feest",
            "type": "MID",
            "children": [
                {
                    "id": "092c279a-baaa-4c1a-9656-7ecd5d2f3c97",
                    "name": "Max Power",
                    "type": "LOW",
                    "children": [
                        {
                            "id": "0925279a-baaa-4c1a-9656-7ecd5d2f3c97",
                            "name": "Max Lower",
                            "type": "LOW",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "id": "2fb8e01c-ffc6-4535-8241-ad057946961a",
            "name": "Gladys Jast",
            "type": "MID",
            "children": []
        }
    ]
}

thx for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏辞 2025-01-26 07:31:04

这是一个非常普遍的编程问题。如果没有一些实际的代码,你会被骂,而不仅仅是向我们展示数据,但这里有一些一般性的想法,可以帮助你想出一些代码,如果你仍然需要帮助,然后将其发布。

基本上,您只需要跟踪深度变量即可。父节点的深度为 0,其子节点的深度为 1。当您遍历树时,请记录您的深度。当您转到子节点时,增加深度;当您转到父节点时,减少深度。在每一步中,根据某个 max_depth 变量检查您的深度,如果您想要的深度,请不要转到任何子节点。

可能有一些方法可以让您更轻松地做到这一点,但您至少可以通过这种方式提供一些起始代码,以便人们可以提供建议。

This is a pretty general programming problem. You'll get yelled at without some actual code and not just showing us the data, but here are some general thoughts to help you come up with some code, and then post it if you still need help.

Basically you just need to keep track of a depth variable. The parent node has a depth of 0 and its children have a depth of 1. When you're traversing the tree keep track of your depth. When you go to a child node, increment depth and when you go to a parent node, decrement it. At each step, check your depth against some max_depth variable, and if you're as deep as you wanna go, don't go to any child nodes.

There may be methods that let you do this more easily but you can at least offer some starting code this way that people can offer suggestions on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文