简体   繁体   中英

Retrofit/GSON Is not parsing my JSON array of object

I am getting null objects or GSON won't parse even tho I serialized. Here is the model of JSON data I am trying to parse:

[
{
    "parent": {
        "title": "Home"
    }
},
{
    "parent": {
        "title": "Learn",
        "children": [
            {
                "category": {
                    "title": "Computer Science",
                    "child": [
                        {
                            "title": "Database Management System",
                            "url": "taxonomy/term/57"
                        },
                        {
                            "title": "Programming Language",
                            "url": "taxonomy/term/118"
                        }
                    ]
                }
            },
            {
                "category": {
                    "title": "General",
                    "child": [
                        {
                            "title": "Mathematics",
                            "url": "taxonomy/term/115"
                        }
                    ]
                }
            }
        ]
    }
}]

Here is the model class:

public class Parent {
@SerializedName("title")
private String title;
@SerializedName("children")
List<children> childrenList;

public List<children> getChildrenList() {
    return childrenList;
}

public String getTitle() {
    return title;
}

public void setTitle(String title){
    this.title = title;
}

public class Category{
    @SerializedName("title")
    String title;

    @SerializedName("url")
    String url;
}

public class children{
    @SerializedName("category")
    List<Category> categorylist;
}
}

Here is the service class:

public interface DynamicMenuService {
@POST("http://dev.myurl.com/somepath")
Call<List<Parent>> getMenuData();
}

I can confirm that the end point is returning the data (I added a logging interceptor to retrofit) every field is null. Including the title in the parent class is null but the responce.body() in onResponce returns correct number of parent objects (that is the same number as the JSON parent objects) the fields in the responce parent objects are null.

Try add a wrapper for your parent class, like this:

public class ParentsWrapper {
   @SerializeName("parent")
   private  Parent [] parents;
}

so your interface method should return a list of ParentsWapper objects, like so:

public interface DynamicMenuService {
    @POST("http://dev.myurl.com/somepath")
    Call<List<ParentsWrapper>> getMenuData();
}

You'll see that some of your classes are absolutely wrong, fix that too.

public class Parent
{
    @SerializeName("title")
    private String title;

    @SerializeName("children")
    private Children[] children;

    public String getTitle ()
    {
       return title;
    }

    public void setTitle (String title)
    {
        this.title = title;
    }

    public Children[] getChildren ()
    {
        return children;
    }

    public void setChildren (Children[] children)
    {
        this.children = children;
    }

}

public class Children
{
    @SerializeName("category")
    private Category category;

    public Category getCategory ()
    {
        return category;
    }

    public void setCategory (Category category)
    {
        this.category = category;
    }

}

public class Child
{
    @SerializeName("title")
    private String title;

    @SerializeName("url")
    private String url;

    public String getTitle ()
    {
        return title;
    }

    public void setTitle (String title)
    {
        this.title = title;
    }

    public String getUrl ()
    {
        return url;
    }

    public void setUrl (String url)
    {
        this.url = url;
    }

}

public class Category
{
    @SerializeName("child")
    private Child[] child;

    @SerializeName("title")
    private String title;

    public Child[] getChild ()
    {
        return child;
    }

    public void setChild (Child[] child)
    {
        this.child = child;
    }

    public String getTitle ()
    {
        return title;
    }

    public void setTitle (String title)
    {
        this.title = title;
    }

}

http://www.jsonschema2pojo.org/ use this for data modelling. Just paste your response from postman there and get your models.

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