使用房间体系结构为嵌套的 JSON 对象创建表



我有一个JSON,它有一个嵌套的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"]
}
}
}
}
}

我只需要页面数据。所以我混淆了如何在存储在数据库中时消除其余数据。我还想知道如何为嵌套的 JSON 对象创建实体类。

这是我的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;
}
}

在此之后,我不知道如何进一步进行。

最后,经过2 个小时的挖掘,我得到了答案,答案是您可以使用@Embedded。以下是我的工作解决方案:

@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(){
}

条款.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;
}
}

缩略图.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;
}
}

嵌入将在 XYZ 表中创建一个额外的列,其中包含说明、高度、源、宽度。通过使用@Embedded您可以直接从数据库中获取具有一对多关系的对象。

您可以尝试使用一些更复杂的 JSON,并尝试围绕它构建一个实体类。

最新更新