【发布时间】:2019-11-21 02:25:24
【问题描述】:
是否可以转换:
{
"news": [
{
"nid": 1,
"type": "test1",
},
{
"nid": 2,
"type": "test2",
}
]
}
当我收到 Retrofit 的结果时要这样做吗?
{
"nid": 1,
"type": "test1",
},
{
"nid": 2,
"type": "test2",
}
实际上,在我的模型中,我有一个 NewsList 对象,其中包含一个 News 列表(其中包含值:nid、type)。
Retrofit 查询看起来像这样:
@GET("test/test.json")
suspend fun getListNews(): Response<Newslist>
一切正常,但现在我需要将这些数据保存在带有 Room 的数据库中,如果 Retrofit 查询是这样的会更容易:
@GET("test/test.json")
suspend fun getListNews(): Response<List<News>
实现这一目标的最佳方法是什么?
- 转换 Json 响应以获得类似上面的内容?
- 保留这样的内容并将 NewsList 转换为能够存储在房间数据库中?
【问题讨论】:
-
如果
Newslist对象包含News的列表,为什么不能使用Newslist.news获取News的列表
标签: android kotlin retrofit2 android-room