Reputation: 1093
I have a JSON which has a nested JSON objects. I don't need all the data. I tried to create entity class by following the official document but got lost in the middle.
JSON:
{
"batchcomplete": "",
"continue": {
"gpsoffset": 10,
"continue": "gpsoffset||"
},
"query": {
"pages": {
"23729925": {
"pageid": 23729925,
"ns": 0,
"title": "Amir",
"index": 1
},
"31726102": {
"pageid": 31726102,
"ns": 0,
"title": "Amir Abedzadeh",
"index": 5,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Amir_Abedzadeh_in_Iran_national_football_team.jpg/36px-Amir_Abedzadeh_in_Iran_national_football_team.jpg",
"width": 36,
"height": 50
},
"terms": {
"description": ["Iranian footballer"]
}
},
"32174830": {
"pageid": 32174830,
"ns": 0,
"title": "Amir Abrashi",
"index": 6,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7b/20160326_AUT_ALB_9815_%28cropped%29.jpg/25px-20160326_AUT_ALB_9815_%28cropped%29.jpg",
"width": 25,
"height": 50
},
"terms": {
"description": ["Albanian footballer"]
}
},
"32342708": {
"pageid": 32342708,
"ns": 0,
"title": "Amir Blumenfeld",
"index": 9,
"terms": {
"description": ["Israeli American comedian"]
}
},
"34186501": {
"pageid": 34186501,
"ns": 0,
"title": "Amir Hamzah",
"index": 10,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Amir_Hamzah_portrait_edit.jpg/33px-Amir_Hamzah_portrait_edit.jpg",
"width": 33,
"height": 50
},
"terms": {
"description": ["Indonesian poet"]
}
},
"2180899": {
"pageid": 2180899,
"ns": 0,
"title": "Amir Johnson",
"index": 8,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Amir_Johnson_%2834461581555%29.jpg/32px-Amir_Johnson_%2834461581555%29.jpg",
"width": 32,
"height": 50
},
"terms": {
"description": ["American professional basketball player"]
}
},
"1290366": {
"pageid": 1290366,
"ns": 0,
"title": "Amir Khan (boxer)",
"index": 2,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Amir_Khan.jpg/40px-Amir_Khan.jpg",
"width": 40,
"height": 50
},
"terms": {
"description": ["British boxer"]
}
},
"517348": {
"pageid": 517348,
"ns": 0,
"title": "Amir Khusrow",
"index": 3,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Amir_Khusro.jpg/50px-Amir_Khusro.jpg",
"width": 50,
"height": 38
},
"terms": {
"description": ["Indian poet, writer, musician and scholar"]
}
},
"8568334": {
"pageid": 8568334,
"ns": 0,
"title": "Amiri Baraka",
"index": 4,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Amiri_Baraka_2013.jpg/35px-Amiri_Baraka_2013.jpg",
"width": 35,
"height": 50
},
"terms": {
"description": ["African-American writer"]
}
},
"852331": {
"pageid": 852331,
"ns": 0,
"title": "Amirkabir University of Technology",
"index": 7,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/AKUT.svg/41px-AKUT.svg.png",
"width": 41,
"height": 50
},
"terms": {
"description": ["public, research university located in Tehran, Iran"]
}
}
}
}
}
I only need pages data. So I ma confuse how to eliminate rest of the data while storing in db. I am also wanted to know how to create the entity class for the nested JSON objects.
Here is my PageDetailEntity:
@Entity(tableName = "ResponseEntity")
public class PageDetailEntity {
@Ignore
public PageDetailEntity( boolean batchcomplete, List<ContinueModel> mContinue, List<QueryModel>queryModel) {
this.batchcomplete = batchcomplete;
this.mContinue = mContinue;
this.queryModel = queryModel;
}
public PageDetailEntity( int id, boolean batchcomplete, List<ContinueModel> mContinue, List<QueryModel>queryModel) {
this.id = id;
this.batchcomplete = batchcomplete;
this.mContinue = mContinue;
this.queryModel = queryModel;
}
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
@Ignore
@ColumnInfo(name = "batchcomplete")
private boolean batchcomplete;
@Ignore
@TypeConverters(ContinueModelConverter.class)
public List<ContinueModel> mContinue;
@TypeConverters(QueryModelConverter.class)
private List<QueryModel>queryModel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isBatchcomplete() {
return batchcomplete;
}
public void setBatchcomplete(boolean batchcomplete) {
this.batchcomplete = batchcomplete;
}
public List<ContinueModel> getmContinue() {
return mContinue;
}
public void setmContinue(List<ContinueModel> mContinue) {
this.mContinue = mContinue;
}
public List<QueryModel> getQueryModel() {
return queryModel;
}
public void setQueryModel(List<QueryModel> queryModel) {
this.queryModel = queryModel;
}
}
After this I am not getting any idea how to proceed further.
Upvotes: 3
Views: 1891
Reputation: 1093
Finally after digging for 2 hours I got the answer and the answer is you can use @Embedded. Below is my working solution:
@Entity(tableName = "XYZ")
public class PageEntity {
@PrimaryKey(autoGenerate = true)
private int page_id;
private String title;
private int index;
@Embedded
private Thumbnail thumbnail;
@Embedded
private Terms terms;
public PageEntity(){
}
Terms.java
public class Terms {
public ArrayList<String>description = new ArrayList<String>();
@Ignore
public Terms(){
}
public Terms(ArrayList<String> description){
this.description.addAll(description);
}
public ArrayList<String> getDescription() {
return description;
}
public void setDescription(ArrayList<String> description) {
this.description = description;
}
}
Thumbnail.java
public class Thumbnail {
private String source;
private int width,height;
@Ignore
public Thumbnail(){
}
public Thumbnail(String source, int width, int height){
this.height=height;
this.source=source;
this.width=width;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Embedded will create an extra columns in your XYZ table with description, height, source, width. By using @Embedded you can get your object with your OneToMany relation directly from the database.
You can try out with some more complex JSON and try to build an Entity class around it.
Upvotes: 4