Reputation: 3
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.
Upvotes: 0
Views: 1059
Reputation: 64
http://www.jsonschema2pojo.org/ use this for data modelling. Just paste your response from postman there and get your models.
Upvotes: 1
Reputation: 714
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;
}
}
Upvotes: 1