Parsing JSON in Kotlin Multiplatform:io.ktor.serialization.JsonConvertException:非法输入

huangapple go评论67阅读模式
英文:

Parsing JSON in Kotlin Multiplatform : io.ktor.serialization.JsonConvertException: Illegal input

问题

以下是您提供的代码的中文翻译:

我正在初次接触Kotlin Multiplatform,并且正在按照这里的指南进行操作:

https://kotlinlang.org/docs/multiplatform-mobile-upgrade-app.html

我有我的类

@Serializable
data class RocketLaunch (
    @SerialName("flight_number")
    val flightNumber: Int,
    @SerialName("name")
    val missionName: String,
    @SerialName("date_utc")
    val launchDateUTC: String,
    @SerialName("success")
    val launchSuccess: Boolean?,
)

我实例化了一个HTTPClient并按照指南中的详细信息调用了SpaceX的URL

private val httpClient = HttpClient {
        install(ContentNegotiation) {
            json(Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
            })
        }
    }

    @Throws(Exception::class)
    suspend fun greet(): String {
        val rockets: List<RocketLaunch> = httpClient.get("https://api.spacexdata.com/v4/launches").body()
        val lastSuccessLaunch = rockets.first() //{ it.launchSuccess == true }
        return "Guess what it is! > ${platform.name.reversed()}!"
                "\nThe last successful launch was ${lastSuccessLaunch.title} 🚀"
    }

当我运行它并捕获异常时,我得到了以下错误:

kotlinx.serialization.json.internal.JsonDecodingException: 期望将类kotlinx.serialization.json.JsonObject(不可用Kotlin反射)作为kotlinx.serialization.Polymorphic的序列化主体,但实际上是类kotlinx.serialization.json.JsonArray(不可用Kotlin反射)

我已经仔细检查过,我正在正确地遵循指南,并且数据源有效。

插件

kotlin("plugin.serialization") version "1.8.10"

依赖项

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")

implementation("io.ktor:ktor-client-core:$ktorVersion")

implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")

implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
英文:

I am taking my first look at Kotlin Multiplatform and am following the guide here

https://kotlinlang.org/docs/multiplatform-mobile-upgrade-app.html

I have my class

@Serializable
data class RocketLaunch (
    @SerialName(&quot;flight_number&quot;)
    val flightNumber: Int,
    @SerialName(&quot;name&quot;)
    val missionName: String,
    @SerialName(&quot;date_utc&quot;)
    val launchDateUTC: String,
    @SerialName(&quot;success&quot;)
    val launchSuccess: Boolean?,
)

I instantiate a HTTPClient and call the SpaceX URL as detailed in the guide

private val httpClient = HttpClient {
        install(ContentNegotiation) {
            json(Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
            })
        }
    }

    @Throws(Exception::class)
    suspend fun greet(): String {
        val rockets: List&lt;RocketLaunch&gt; = httpClient.get(&quot;https://api.spacexdata.com/v4/launches&quot;).body()
        val lastSuccessLaunch = rockets.first() //{ it.launchSuccess == true }
        return &quot;Guess what it is! &gt; ${platform.name.reversed()}!&quot;
                &quot;\nThe last successful launch was ${lastSuccessLaunch.title} &#128640;&quot;
    }

And when I run it and catch the exception I get

> kotlinx.serialization.json.internal.JsonDecodingException: Expected
> class kotlinx.serialization.json.JsonObject (Kotlin reflection is not
> available) as the serialized body of
> kotlinx.serialization.Polymorphic<List>, but had class
> kotlinx.serialization.json.JsonArray (Kotlin reflection is not
> available)

I have double checked and I am following the guide properly, and the feed works

Plugin

kotlin("plugin.serialization") version "1.8.10"

Dependancies

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")

implementation("io.ktor:ktor-client-core:$ktorVersion")

implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")

implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")

答案1

得分: 2

修复方法是添加以下代码:

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("kotlinx-serialization")
}

到共享的 build.gradle.kts 文件中。这看起来很奇怪,因为它已经添加到了项目级别的文件中,如下所示:

kotlin("plugin.serialization") version "1.8.10"
英文:

The fix was to add

id("kotlinx-serialization") e.g.

plugins {
    kotlin(&quot;multiplatform&quot;)
    id(&quot;com.android.library&quot;)
    id(&quot;kotlinx-serialization&quot;)
}

To the shared build.gradle.kts file. I find this odd as it was added to the Project level one as shown below.

kotlin("plugin.serialization") version "1.8.10"

答案2

得分: 1

I ran into the same error, what I did was to change kotlin("plugin.serialization") version "1.8.21" to id("org.jetbrains.kotlin.plugin.serialization") version "1.8.21" in shared build.gradle.kts, and the problem was solved.

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("org.jetbrains.kotlin.plugin.serialization") version "1.8.21"
}
英文:

I ran into the same error, what I did was to change kotlin(&quot;plugin.serialization&quot;) version &quot;1.8.21&quot; to id(&quot;org.jetbrains.kotlin.plugin.serialization&quot;) version &quot;1.8.21&quot; in shared build.gradle.kts, and the problem was solved.

plugins {
    kotlin(&quot;multiplatform&quot;)
    id(&quot;com.android.library&quot;)
    id(&quot;org.jetbrains.kotlin.plugin.serialization&quot;) version &quot;1.8.21&quot;
}

huangapple
  • 本文由 发表于 2023年4月19日 17:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定