Reputation: 61
I am trying to make a pojo class for recyclerview.
The data I am getting from the json will be like this.
newsfeeddata:{ id:"",
timestamp:"",
userdetails:{
id:""
profile_pic_url:"",
name:""
},
post:{ (optional)
id:""
text:""
},
media :{ (optional)
id:""
url:""
}
}
In some objects there will be 'post' and in other object instead of 'post' there will be 'media'. How do I make a pojo class for this?
Upvotes: 0
Views: 1406
Reputation: 2195
Make separate classes for userdetails
, post
and media
. And use them as instance variable in newsfeeddata
class.
public class Post {
public String id;
public String text;
}
public class Userdetails {
public String id;
public String profile_pic_url;
public String name;
}
public class Media {
public String id;
public String url;
}
Now use an instance of these in your newsfeeddata
class.
public class Newsfeeddata {
public String id;
public String timestamp;
public Userdetails userdetails;
public Post post;
public Media media;
}
Note:
You can change the access-modifier to private and use getters and setters. Read about lombook-data annotation. Using a single @Data
annotation above your class, you can have all getters, setters, toString implementation and more. Makes your class concise and pretty.
You may want to change data-type of fields. For simplicity, I have used String
.
Upvotes: 2