Android Moshi和2:解析根元素的内容
从今天起,我收到了如下json响应:Android Moshi和2:解析根元素的内容,android,json,kotlin,retrofit2,moshi,Android,Json,Kotlin,Retrofit2,Moshi,从今天起,我收到了如下json响应: { "status" : "Ok", "otherField" : "Somevalues" } 我把它映射到像 data class MyResponse( val status : String, val otherField : String ) 现在,响应结构发生了如下变化 { "rootElement" : { "status" : "Ok", "otherField" : "So
{
"status" : "Ok",
"otherField" : "Somevalues"
}
我把它映射到像
data class MyResponse(
val status : String,
val otherField : String
)
现在,响应结构发生了如下变化
{
"rootElement" : {
"status" : "Ok",
"otherField" : "Somevalues"
}
}
有一种方法可以告诉Moshi直接解析“rootElement”的内容,而不改变“MyResponse”结构
有一种方法可以告诉Moshi直接解析“rootElement”的内容,而不改变“MyResponse”结构
如果我正确理解了您的需求,那么您可以解析rootElement
的内容,而无需更改MyResponse
数据类。再创建一个Kotlin数据类,如下所示
data class ResponseRoot (
val rootElement : MyResponse
)
并将此ResponseRoot
数据类返回类型用于改装响应,而不是MyResponse
类
在API接口中,只需进行更改
@GET("url/here/")
fun fooBar(/* paramters if there any */): Call<ResponseRoot>
@GET(“url/here/”)
fun fooBar(/*参数,如果有*/):调用