[英]Moshi and retrofit2: parse content of root element
Since today i've received json responses like:从今天开始,我收到了 json 响应,例如:
{
"status" : "Ok",
"otherField" : "Somevalues"
}
That i map into classes like我映射到类中
data class MyResponse(
val status : String,
val otherField : String
)
Now response structure is changed in something like现在响应结构发生了类似的变化
{
"rootElement" : {
"status" : "Ok",
"otherField" : "Somevalues"
}
}
There's a way to tell Moshi to parse directly content of "rootElement" whithout changing "MyResponse" structure?有没有办法告诉 Moshi 在不更改“MyResponse”结构的情况下直接解析“rootElement”的内容?
There's a way to tell Moshi to parse directly content of "rootElement" whithout changing "MyResponse" structure?有没有办法告诉 Moshi 在不更改“MyResponse”结构的情况下直接解析“rootElement”的内容?
If I understood your requirement properly, yes you can parse the content of rootElement
without altering MyResponse
data class.如果我正确理解了您的要求,是的,您可以在不更改MyResponse
数据类的情况下解析rootElement
的内容。 Create one more Kotlin data class like below像下面这样再创建一个 Kotlin 数据类
data class ResponseRoot (
val rootElement : MyResponse
)
And use this ResponseRoot
data class return type for your Retrofit Response instead of MyResponse
class.并将此ResponseRoot
数据类返回类型用于 Retrofit Response 而不是MyResponse
类。
In your API interface just do the changes在您的 API 界面中,只需进行更改
@GET("url/here/")
fun fooBar(/* paramters if there any */): Call<ResponseRoot>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.