简体   繁体   中英

how to deserialize this json file with gson in java?

I can not understand how to import one or more in a hashmap the "name" of each child with their father , and how to create a method that will give me back the father of her child. Can anyone help me?

json

    {
    "child": [
        {
            "name": "one",
            "child": [
                {
                    "id": "0001",
                    "name": "oneone",
                    "image": "one.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",
                    "image": "two.jpg"
                },
                {
                    "id": "onethree",
                    "name": "three",
                    "image": "three.jpg"
                }
            ]
        },
        {
            "name": "two",
            "child": [
                {
                    "id": "0004",
                    "name": "twoone",
                    "image": "one.jpg"
                },
                {
                    "id": "0005",
                    "name": "twotwo",
                    "image": "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",
                    "image": "three.jpg"
                }
            ]
        }

    ]
}

Child

public class Child {

    private String id;
    private String name;
    private String image;
    private List<Child> child = new ArrayList<Child>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image= image;
    }

    public List<Child> getChild() {
        return child;
    }

    public void setChild(List<Child> child) {
        this.child = child;
    }

}

Try this code. To get the parent for a given child use getParentFor method where first arg is a root of the structure and the second is a name of the given child.

public class GsonExample1 {

    interface ExampleVisitor {
        void onParentStart(Example example);

        void onChild(Example example);

    }

    static class Example {

        private String id;
        private String name;
        private String immagine;
        private Example[] child;

        public void accept(ExampleVisitor visitor) {
            visitor.onParentStart(this);

            if (child != null) {
                for (Example c : child) {
                    visitor.onChild(c);
                }

                for (Example c : child) {
                    c.accept(visitor);
                }
            }
        }

        @Override
        public String toString() {
            return "Example [id=" + id + ", name=" + name + ", immagine="
                    + immagine + ", child=" + Arrays.toString(child) + "]";
        }

    }

    public static Example getParentFor(Example root, final String name) {

        final Example[] bag = new Example[1];
        root.accept(new ExampleVisitor() {

            private Example currentParent;

            public void onParentStart(Example example) {
                currentParent = example;
            }

            public void onChild(Example example) {

                if (name.equals(example.name)) {
                    bag[0] = currentParent;
                }

            }
        });
        return bag[0];
    }

    public static Example[] getAllChildrenFor(Example root, final String name) {
        final Example[] bag = new Example[1];
        root.accept(new ExampleVisitor() {

            public void onParentStart(Example example) {
                if (name.equals(example.name)) {
                    bag[0] = example;
                }
            }

            public void onChild(Example example) {

            }
        });
        return bag[0] != null ? bag[0].child : null;
    }

    public static void main(String[] args) {

        GsonExample1.class.getResourceAsStream("sample1.txt");
        Gson gson = new Gson();
        Example root = gson.fromJson(
                new InputStreamReader(GsonExample1.class
                        .getResourceAsStream("sample1.txt")), Example.class);

        System.out.println(root);

        System.out.println(getParentFor(root, "oneoneone"));

        System.out.println(Arrays
                .toString(getAllChildrenFor(root, "oneoneone")));

    }
}

Output

Example [id=null, name=null, immagine=null, child=[Example [id=null, name=one, immagine=null, child=[Example [id=0001, name=oneone, immagine=one.jpg, child=[Example [id=1, name=oneoneone, immagine=null, child=[Example [id=1, name=oneoneoneone, immagine=null, child=[Example [id=1, name=oneoneoneoneone, immagine=null, child=null], Example [id=2, name=oneoneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneonetwo, immagine=null, child=null]]], Example [id=0002, name=onetwo, immagine=two.jpg, child=null], Example [id=onethree, name=three, immagine=three.jpg, child=null]]], Example [id=null, name=two, immagine=null, child=[Example [id=0004, name=twoone, immagine=one.jpg, child=null], Example [id=0005, name=twotwo, immagine=two.jpg, child=[Example [id=1, name=twotwoone, immagine=null, child=[Example [id=1, name=twotwooneone, immagine=null, child=null], Example [id=2, name=twotwoonetwo, immagine=null, child=null], Example [id=3, name=twotwoonethree, immagine=null, child=null]]], Example [id=2, name=twotwotwo, immagine=null, child=null]]], Example [id=0006, name=twothree, immagine=three.jpg, child=null]]]]]

parent for child oneoneone is

Example [id=0001, name=oneone, immagine=one.jpg, child=[Example [id=1, name=oneoneone, immagine=null, child=[Example [id=1, name=oneoneoneone, immagine=null, child=[Example [id=1, name=oneoneoneoneone, immagine=null, child=null], Example [id=2, name=oneoneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneoneonetwo, immagine=null, child=null], Example [id=3, name=oneoneonethree, immagine=null, child=null]]], Example [id=2, name=oneonetwo, immagine=null, child=null]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM