Moshi @Json 注释不适用于 com.github.kittinunf.fuel.moshi.moshiDeserializerOf?

Noa*_*ein 2 kotlin moshi kotlin-fuel

我有一个响应对象:

data class ResponseObject(
        val notCamelcase: String,
        val param2: String,
        val param3: String
)
Run Code Online (Sandbox Code Playgroud)

notCamelCase请注意,响应 JSON 正文中的第一个参数不是驼峰式(例如)。

此外,我使用 FUEL 库触发 REST 调用:

Fuel.get(someParam)
        .responseObject(moshiDeserializerOf(ResponseObject::class.java)) { _, response, result ->
            try {
                if (response.statusCode == HttpURLConnection.HTTP_OK) {
                    val responseObject = result.component1()
        }
Run Code Online (Sandbox Code Playgroud)

以下是我的进口:

import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.core.HttpException
import com.github.kittinunf.fuel.moshi.moshiDeserializerOf
Run Code Online (Sandbox Code Playgroud)

为了在下面的代码中使用驼峰命名法,我修改了 ResponseObject,如下所示:

data class ResponseObject(
        @Json(name="notCamelcase")
        val notCamelCase: String,
        val param2: String,
        val param3: String
)
Run Code Online (Sandbox Code Playgroud)

正如这里所描述的......

在本例中,notCamelCase为空。@Json 是否只能与 com.github.kittinunf.fuel.moshi.moshiDeserializerOf 一起使用?怎么了?

Rax*_*Rax 7

使用@field:Json()Moshi kotlin 的注释。

data class ResponseObject(
    @field:Json(name="notCamelcase")
    val notCamelCase: String,
    val param2: String,
    val param3: String
)
Run Code Online (Sandbox Code Playgroud)

参考: https: //github.com/square/moshi/issues/315 正如讨论中提到的,这仍然是一种解决方法。官方 Kotlin 支持是正确的选择:https://github.com/square/moshi#kotlin