【问题标题】:Finding deeply nested key/value in JSON在 JSON 中查找深度嵌套的键/值
【发布时间】:2013-03-27 20:43:44
【问题描述】:

假设我有一个这样的 JSON 数组:

[
    {
        "id": "429d30a1-9364-4d9a-92e0-a17e00b3afba",
        "children": [],
        "parentid": "",
        "name": "Expo Demo"
    }, 
    {
        "id": "f80f1034-9110-4349-93d8-a17e00c9c317",
        "children": 
            [
                {
                    "id":"b60f2c1d-368b-42c4-b0b2-a1850073e1fe", 
                    "children":[], 
                    "parentid":"f80f1034-9110-4349-93d8-a17e00c9c317", 
                    "name":"Tank"
                }
            ],
        "parentid": "",
        "name": "Fishtank"
    }, 
    {
        "id": "fc8b0697-9406-4bf0-b79c-a185007380b8",
        "children": [
            {
                "id":"5ac52894-4cb6-46c2-a05a-a18500739193", 
                "children":[
                    {
                        "id": "facb264c-0577-4627-94a1-a1850073c270",
                        "children":[
                            {
                                "id":"720472b5-189e-47f1-97a5-a18500a1b7e9", 
                                "children":[], 
                                "parentid":"facb264c-0577-4627-94a1-a1850073c270", 
                                "name":"ubSubSub"
                            }],
                        "parentid": "5ac52894-4cb6-46c2-a05a-a18500739193",
                        "name": "Sub-Sub1"
                    }], 
                "parentid":"fc8b0697-9406-4bf0-b79c-a185007380b8", "name":"Sub"
            },
            {
                "id":"4d024610-a39b-49ce-8581-a18500739a75", 
                "children":[], 
                "parentid":"fc8b0697-9406-4bf0-b79c-a185007380b8", 
                "name":"Sub2"
            }
        ],
        "parentid": "",
        "name": "Herman"
    }, 
    {
        "id": "a5b140c9-9987-4e6d-a883-a18c00726883",
        "children": [
            {
                "id":"fe103303-fd5e-4cd6-81a0-a18c00733737", 
                "children":[], 
                "parentid":"a5b140c9-9987-4e6d-a883-a18c00726883", 
                "name":"Contains Spaces"
            }],
        "parentid": "",
        "name": "Kiosk"
    }
]

不,我想根据 id 找到某个对象,一旦有了,我需要它的孩子和所有孩子的孩子

假设我想找到具有 id 的元素 if 4d024610-a39b-49ce-8581-a18500739a75

这应该找到元素 Sub2

现在它应该产生所有的子元素 id 女巫将是:

facb264c-0577-4627-94a1-a1850073c270
720472b5-189e-47f1-97a5-a18500a1b7e9

假设我会这样做

findElementsChildren("4d024610-a39b-49ce-8581-a18500739a75")

所以我猜它有两个部分,首先找到“父”元素。然后找到它的childrends childrends children等等。

任何帮助将不胜感激!

【问题讨论】:

  • 我可以找到元素,但我不确定如何找到它的子元素。孩子可以无限嵌套。我在 Javascript 中做了同样的事情。
  • 也许您可以尝试使用JsonPath 库来查询结构?
  • javascript 实际上只是找到一个嵌套在 json 数组/对象深处的元素。这更进一步,找到了所有的孩子。我不知道我会怎么做,所以算法
  • 另一个可以很好地遍历此类 JSON“树”的库是 JSON.Simple。对于每个JSONObject,您将进行一次递归调用,检查它是否具有给定的 id,如果有,则通过另一个递归调用返回其子级 - 如有必要。
  • 好问题,终于找到了和我类似的json结构。你可以在这里发布一个真实的工作示例(你说一切都对你有用),因为在被接受的答案中没有方法element.get("children")

标签: java json


【解决方案1】:

你可以使用递归来解决无限嵌套的问题。使用 Gson,它将类​​似于以下代码 sn-p(未测试)。其他库也将提供 JsonElement 结构。

private JsonElement findElementsChildren(JsonElement element, String id) {
    if(element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        if(id.equals(jsonObject.get("id").getAsString())) {
            return jsonObject.get("children");
        } else {
            return findElementsChildren(element.get("children").getAsJsonArray(), id);
        }
    } else if(element.isJsonArray()) {
        JsonArray jsonArray = element.getAsJsonArray();
        for (JsonElement childElement : jsonArray) {
            JsonElement result = findElementsChildren(childElement, id);
            if(result != null) {
                return result;
            }
        }
    }

    return null;
}

【讨论】:

  • 什么是 JsonElement?一个 JSONArray?
  • JsonElement 可以是任何东西,JSON 对象、数组或原语(字符串、整数、..)。
  • 好吧,我稍微改了一下,但基本一样,而且效果很好!谢谢
【解决方案2】:

根据 Stefan Jansen 的回答,我做了一些更改,这就是我现在所拥有的:

nestedChildren 全局声明,在子元素被搜索之前重置为 new ArrayList()

private void findAllChild(JSONArray array) throws JSONException {

    for ( int i=0;i<array.length();i++ ) {
        JSONObject json = array.getJSONObject(i);
        JSONArray json_array = new JSONArray(json.getString("children"));
        nestedChildren.add(json.getString("id"));
        if ( json_array.length() > 0 ) {
            findAllChild(json_array);
        }
    }
}

这假设它是所有数组,在我的情况下它是

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2020-12-10
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多