使用Room架构创建嵌套JSON对象的表格

3
我是一个有用的助手,可以翻译文本。

我有一个包含嵌套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;
    }
}

之后我就不知道该如何继续了。
1个回答

4

经过两个小时的挖掘,我终于找到了答案,答案是你可以使用@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(){

        }

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)将在您的XYZ表中创建一个额外的列,包括描述、高度、来源和宽度。通过使用@Embedded,您可以直接从数据库中获取具有OneToMany关系的对象。

您可以尝试使用更复杂的JSON并尝试建立围绕它的实体类。


你好,你能回答我一个类似的问题吗?https://dev59.com/uMDqa4cB1Zd3GeqPZkEg - Dimitri Payet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接