目前,我有一个JSON,基本上可以有一个“无限”的深度。有没有可能我可以限制深度,只得到像JSON的前两个级别?也许甚至像Jackson或GSON的库?
当前的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": []
}
]
}
谢谢你的帮助
1条答案
按热度按时间ifmq2ha21#
这是一个相当普遍的编程问题。如果你没有一些实际的代码,而不仅仅是给我们看数据,你会被责骂,但是这里有一些一般的想法来帮助你想出一些代码,然后如果你仍然需要帮助的话,把它发布出来。
基本上,你只需要跟踪一个
depth
变量。父节点的深度为0,其子节点的深度为1。当你遍历树时,跟踪你的深度。当你到达一个子节点时,增加深度,当你到达一个父节点时,减少深度。在每一步中,根据某个max_depth
变量检查你的深度。如果你想去的那么深,就不要去任何子节点。也许有一些方法可以让你更容易地做到这一点,但你至少可以提供一些启动代码,这样人们就可以提供建议。