英文:
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("flight_number")
val flightNumber: Int,
@SerialName("name")
val missionName: String,
@SerialName("date_utc")
val launchDateUTC: String,
@SerialName("success")
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<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} 🚀"
}
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("multiplatform")
id("com.android.library")
id("kotlinx-serialization")
}
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("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"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论