简体   繁体   中英

Deserializing JSON strings using GSON in Android

This might be a bit long so please bear with me..

I'm working on a project in Android. I have a JSON string that i need to deserialize into objects representing the JSON structure, the JSON string is something like below:

{"parents":[
    {"parent":{
            "id":1,
            "name":"Sam",
            "childs":[
                {
                    "child":{
                        "id":1,
                        "name":"Alice",
                        "books":[
                            {
                                "book":{
                                    "id":1,
                                    "name":"Alice in Wonderland"
                                }
                            }
                        ]
                    }
                }
            ]
        } 

    }    
]}

Note that for arrays there are repeating values (ie many parent and many child) i just put one line here to make it short.

With this i created classes to accomodate the structure:

public class Containers{
    private List<Parent> parents;
    //setter and getter..
}

public static class Parent extends CommonItem {
    private List<Child> childs;
    //setter and getter..
}

public static class Child extends CommonItem {
    private List<Book> books;
    //setter and getter..
}

public class CommonItem{
    private int id;
    private String name;
    //setter and getter
}

Note that i'd created a common class to accommodate the repeating attributes. Below is how i parse them using GSON:

Gson gson = new Gson();
Containers parents = gson.fromJson(jsonString, Containers.class);

After parsing (there're no error) i try to retrieve the objects. The number of object count in the parents list is correct (ie there are 5 parents in the JSON string, and there are 5 Parent objects in the parents List). However, all their attributes are not there, and the child tree are missing as well.

I'd tried numerous configuration, including remove the extended class and put all the id and name in the respective classes but the result is still the same.

Anyone can give me any pointer that where did i go wrong?

Thanx a bunch!

Your JSON and Java class mismatch.

Lets read

{"parents":[
    {"parent":{
            "id":1,
            "name":"Sam",
            "childs":[
                {
                    "child":{
                        "id":1,
                        "name":"Alice",
                        "books":[
                            {
                                "book":{
                                    "id":1,
                                    "name":"Alice in Wonderland"
                                }
                            }
                        ]
                    }
                }
            ]
        } 

    }    
]}

Your top object has a List of class that has parents as it's attribute. Your Java class Container has parents attribute. Good.

Now this list parents is a List of objects that has parent attribute. Does your Parent object has parent attribute? No.

Go further down, each Parent has id , name , and a List of objects as childs (children, should be) attribute that has child attribute, does your Child class has it? No.

Similarly, goes for books .

Did you try your classes on JSON like

{"parents":[
        {
            "id":1,
            "name":"Sam",
            "childs":[
                    {
                        "id":1,
                        "name":"Alice",
                        "books":[
                            {
                                    "id":1,
                                    "name":"Alice in Wonderland"
                            }
                        ]
                    }

            ]
        }  
]}

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