gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:36:22 25 4
gpt4 key购买 nike

我有一个响应对象:

data class ResponseObject(
val notCamelcase: String,
val param2: String,
val param3: String
)

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

Furtheron,我触发了 REST 调用 with the FUEL library :

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

以下是我的导入:

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

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

data class ResponseObject(
@Json(name="notCamelcase")
val notCamelCase: String,
val param2: String,
val param3: String
)

因为它 is described here....

在这种情况下,notCamelCase 为空。 @Json 是否仅与 com.github.kittinunf.fuel.moshi.moshiDeserializerOf 一起工作?怎么了?

最佳答案

为 Moshi kotlin 使用 @field:Json() 注解。

data class ResponseObject(
@field:Json(name="notCamelcase")
val notCamelCase: String,
val param2: String,
val param3: String
)

引用:https://github.com/square/moshi/issues/315如讨论中所述,这仍然是一种解决方法。官方 Kotlin 支持是正确的方式:https://github.com/square/moshi#kotlin

关于kotlin - Moshi @Json 注释不适用于 com.github.kittinunf.fuel.moshi.moshiDeserializerOf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56059579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com