我如何将他们的孩子与他的头部联系起来? Gson Java

时间:2014-11-04 17:13:52

标签: java json hashmap gson jsonobject

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

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]}

1 个答案:

答案 0 :(得分: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方法递归设置为子项。所以每个孩子都知道它的父母。