【问题标题】:How do I associate their child with his header ? Gson Java我如何将他们的孩子与他的标题联系起来?格森爪哇
【发布时间】:2014-12-31 16:38:00
【问题描述】:

这是我创建的方法,一切都很好但我无法将他的父亲与HashMap中的孩子联系起来hm。我希望有人帮助我。谢谢。

public void findChild(JsonArray jarray) {
        ArrayList<String> listChild = new ArrayList<String>();
        HashMap<String, List<String>> hm = new HashMap<String, List<String>>();

        String str = null;

        JsonElement element = null;
        JsonElement e;

        for (int j = 0; j < jarray.size(); j++) {
            e = jarray.get(j).getAsJsonObject().get("name");
            JsonObject jsonObj = jarray.get(j).getAsJsonObject();
            JsonArray jsonArray = jsonObj.getAsJsonArray("child");
            element = jsonObj.get("name");

            if (jsonArray.size() > 0) {

                str = e.getAsString();

                findAllChild(jsonArray);

            }

            listChild.add(element.getAsString());

        }

        hm.put(str, listChild);

        System.err.println("Hash: " + hm);

    }

这是我的 json 文件:

"name": "one",
"id":"YO",
    "child": [
        {
            "name": "one",
            "child": [
                {
                    "id": "0001",
                    "name": "oneone",
                    "photo": "primo.jpg",
                    "child": [
                        {
                            "id": "1",
                            "name": "oneoneone",
                            "child": [
                                {
                                    "id": "1",
                                    "name": "oneoneoneone",
                                    "child": [
                                        {
                                            "id": "1",
                                            "name": "oneoneoneoneone"
                                        },
                                        {
                                            "id": "2",
                                            "name": "oneoneoneonetwo"
                                        },
                                        {
                                            "id": "3",
                                            "name": "oneoneoneonethree"
                                        }
                                    ]
                                },
                                {
                                    "id": "2",
                                    "name": "oneoneonetwo"
                                },
                                {
                                    "id": "3",
                                    "name": "oneoneonethree"
                                }
                            ]
                        },
                        {
                            "id": "2",
                            "name": "oneonetwo"
                        }
                    ]
                },
                {
                    "id": "0002",
                    "name": "onetwo",
                    "photo": "secondo.jpg"
                },
                {
                    "id": "onethree",
                    "name": "terzo",
                    "photo": "terzo.jpg"
                }
            ]
        },
        {
            "name": "two",
            "child": [
                {
                    "id": "0004",
                    "name": "twoone",
                    "photo": "one.jpg"
                },
                {
                    "id": "0005",
                    "name": "twotwo",
                    "photo": "two.jpg",
                    "child": [
                        {
                            "id": "1",
                            "name": "twotwoone",
                            "child": [
                                {
                                    "id": "1",
                                    "name": "twotwooneone"
                                },
                                {
                                    "id": "2",
                                    "name": "twotwoonetwo"
                                },
                                {
                                    "id": "3",
                                    "name": "twotwoonethree"
                                }
                            ]
                        },
                        {
                            "id": "2",
                            "name": "twotwotwo"
                        }
                    ]
                },
                {
                    "id": "0006",
                    "name": "twothree",
                    "photo": "three.jpg"
                }
            ]
        }

    ]
}

这是我的回应:

Hash: {null=[oneoneoneoneone, oneoneoneonetwo, oneoneoneonethree]}
Hash: {oneoneoneone=[oneoneoneone, oneoneonetwo, oneoneonethree]}
Hash: {oneoneone=[oneoneone, oneonetwo]}
Hash: {oneone=[oneone, onetwo, terzo]}
Hash: {null=[twotwooneone, twotwoonetwo, twotwoonethree]}
Hash: {twotwoone=[twotwoone, twotwotwo]}
Hash: {twotwo=[twoone, twotwo, twothree]}
Hash: {two=[one, two]}

【问题讨论】:

    标签: java json hashmap gson jsonobject


    【解决方案1】:

    如果你想将你的 json 字符串反序列化为一个 java 对象,下面的简单代码可能会对你有所帮助。

    此外,从这个答案下面的评论中,我了解到您需要保留对孩子的父母的参考。

    1) 定义一个 pojo:

    public class Item {
        private String name;
        private String id;
        private String photo;
        private Item[] child;
        private Item parent;
    
        public void defineParent(Item parent) {
            this.parent = parent;
            if(this.child != null && this.child.length > 0) {
                for (Item currentChild : this.child) {
                    currentChild.defineParent(this);
                }
            }
        }
    
        // TODO: getters / setters
    }
    

    2) 将你的 json 反序列化到其中:

        String json = "{\"name\":\"one\",\"id\":\"YO\",\"child\":[{\"name\":\"one\",\"child\":[{\"id\":\"0001\",\"name\":\"oneone\",\"photo\":\"primo.jpg\",\"child\":[{\"id\":\"1\",\"name\":\"oneoneone\",\"child\":[{\"id\":\"1\",\"name\":\"oneoneoneone\",\"child\":[{\"id\":\"1\",\"name\":\"oneoneoneoneone\"},{\"id\":\"2\",\"name\":\"oneoneoneonetwo\"},{\"id\":\"3\",\"name\":\"oneoneoneonethree\"}]},{\"id\":\"2\",\"name\":\"oneoneonetwo\"},{\"id\":\"3\",\"name\":\"oneoneonethree\"}]},{\"id\":\"2\",\"name\":\"oneonetwo\"}]},{\"id\":\"0002\",\"name\":\"onetwo\",\"photo\":\"secondo.jpg\"},{\"id\":\"onethree\",\"name\":\"terzo\",\"photo\":\"terzo.jpg\"}]},{\"name\":\"two\",\"child\":[{\"id\":\"0004\",\"name\":\"twoone\",\"photo\":\"one.jpg\"},{\"id\":\"0005\",\"name\":\"twotwo\",\"photo\":\"two.jpg\",\"child\":[{\"id\":\"1\",\"name\":\"twotwoone\",\"child\":[{\"id\":\"1\",\"name\":\"twotwooneone\"},{\"id\":\"2\",\"name\":\"twotwoonetwo\"},{\"id\":\"3\",\"name\":\"twotwoonethree\"}]},{\"id\":\"2\",\"name\":\"twotwotwo\"}]},{\"id\":\"0006\",\"name\":\"twothree\",\"photo\":\"three.jpg\"}]}]}";
        Gson gson = new Gson();
        Item item = gson.fromJson(json, Item.class);
        item.defineParent(null);
    

    编辑:父母将通过defineParent方法递归地设置孩子。所以每个孩子都知道它的父母。

    【讨论】:

    • 感谢您的回复,但是我已经为您定义了一个 POJO,我的 json 文件来加载资产,问题是我有您必须从我的 json 文件自动创建的菜单和为此,我需要在父子之间创建引用,以便在我点击孩子时进入一个特殊的 bean,如果它有的话,它会变成他的孩子。对不起我的英语。
    • @Nick 很难理解你到底需要什么,但我想你希望每个孩子都知道它的父母,这样你就可以通过父母对孩子和孩子对父母。对不起,如果我误解了你。
    • 完全是@DevrimTuncer
    • @Nick 您可以在孩子处保留对父母的引用。不需要特殊的豆子。请参阅我编辑的答案。
    猜你喜欢
    • 2013-10-05
    • 2010-09-12
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多