[英]Moshi and retrofit2: parse content of root element
从今天开始,我收到了 json 响应,例如:
{
"status" : "Ok",
"otherField" : "Somevalues"
}
我映射到类中
data class MyResponse(
val status : String,
val otherField : String
)
现在响应结构发生了类似的变化
{
"rootElement" : {
"status" : "Ok",
"otherField" : "Somevalues"
}
}
有没有办法告诉 Moshi 在不更改“MyResponse”结构的情况下直接解析“rootElement”的内容?
有没有办法告诉 Moshi 在不更改“MyResponse”结构的情况下直接解析“rootElement”的内容?
如果我正确理解了您的要求,是的,您可以在不更改MyResponse
数据类的情况下解析rootElement
的内容。 像下面这样再创建一个 Kotlin 数据类
data class ResponseRoot (
val rootElement : MyResponse
)
并将此ResponseRoot
数据类返回类型用于 Retrofit Response 而不是MyResponse
类。
在您的 API 界面中,只需进行更改
@GET("url/here/")
fun fooBar(/* paramters if there any */): Call<ResponseRoot>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.