Json 序列化错误应为数组“[”的开头,但有 EOF
Json Serialization error expected start of the array "[" but had EOF instead
我目前正在学习 Kotlin Multiplatform,我正在尝试使用 ktor 框架序列化 Json。我从以下 api 收到 JSON:
https://opentdb.com/api.php?amount=10
但我收到此错误:
"error: Expected start of the array "\[" but had "EOF" instead. JSON input: .....answers":\["Patrick Swayze","John Cusack","Harrison Ford"\]}\]}"
我收到的 JSON 看起来像这样:
{ "response_code": 0, "results": [ { "category": "Entertainment: Film", "type": "multiple", "difficulty": "easy", "question": "What breed of dog was Marley in the film "Marley & Me" (2008)?", "correct_answer": "Labrador Retriever", "incorrect_answers": [ "Golden Retriever", "Dalmatian", "Shiba Inu" ] }, { "category": "Entertainment: Comics", "type": "multiple", "difficulty": "hard", "question": "In the Batman comics, by what other name is the villain Dr. Jonathan Crane known?", "correct_answer": "Scarecrow", "incorrect_answers": [ "Bane", "Calendar Man", "Clayface" ] }, { "category": "Entertainment: Film", "type": "boolean", "difficulty": "easy", "question": "Han Solo's co-pilot and best friend, "Chewbacca", is an Ewok.", "correct_answer": "False", "incorrect_answers": [ "True" ] } ] }
这就是我的代码的样子
`@可序列化
数据class你好(
val类别:字符串,
val 类型:布尔值,
val难度:字符串,
val 问题:字符串,
val correctAnswer: 字符串,
val falseAnswer: 字符串
)
class KtorClient {
private val client = HttpClient() {
install(JsonFeature) {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
}
@Throws(Exception::class)
suspend fun returnTestData(): String {
return getTestData().random().question
}
private suspend fun getTestData(): List<Hello> {
return client.get("https://opentdb.com/api.php?amount=10")
}
}`
我做错了什么?
在此先感谢您的回答。
您的数据模型应该如下所示。
data class Response(
val response_code: Int?,
val results: List<Result>?
)
data class Result(
val category: String?,
val correct_answer: String?,
val difficulty: String?,
val incorrect_answers: List<String>?,
val question: String?,
val type: String?
)
private suspend fun getTestData():Response {
return client.get("https://opentdb.com/api.php?amount=10")
}
此外,如果您想为每个字段设置自己的名称,您可以设置
@SerializedName("Field Name")
每个字段。
我目前正在学习 Kotlin Multiplatform,我正在尝试使用 ktor 框架序列化 Json。我从以下 api 收到 JSON:
https://opentdb.com/api.php?amount=10
但我收到此错误:
"error: Expected start of the array "\[" but had "EOF" instead. JSON input: .....answers":\["Patrick Swayze","John Cusack","Harrison Ford"\]}\]}"
我收到的 JSON 看起来像这样:
{ "response_code": 0, "results": [ { "category": "Entertainment: Film", "type": "multiple", "difficulty": "easy", "question": "What breed of dog was Marley in the film "Marley & Me" (2008)?", "correct_answer": "Labrador Retriever", "incorrect_answers": [ "Golden Retriever", "Dalmatian", "Shiba Inu" ] }, { "category": "Entertainment: Comics", "type": "multiple", "difficulty": "hard", "question": "In the Batman comics, by what other name is the villain Dr. Jonathan Crane known?", "correct_answer": "Scarecrow", "incorrect_answers": [ "Bane", "Calendar Man", "Clayface" ] }, { "category": "Entertainment: Film", "type": "boolean", "difficulty": "easy", "question": "Han Solo's co-pilot and best friend, "Chewbacca", is an Ewok.", "correct_answer": "False", "incorrect_answers": [ "True" ] } ] }
这就是我的代码的样子 `@可序列化 数据class你好( val类别:字符串, val 类型:布尔值, val难度:字符串, val 问题:字符串, val correctAnswer: 字符串, val falseAnswer: 字符串 )
class KtorClient {
private val client = HttpClient() {
install(JsonFeature) {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
}
@Throws(Exception::class)
suspend fun returnTestData(): String {
return getTestData().random().question
}
private suspend fun getTestData(): List<Hello> {
return client.get("https://opentdb.com/api.php?amount=10")
}
}`
我做错了什么? 在此先感谢您的回答。
您的数据模型应该如下所示。
data class Response(
val response_code: Int?,
val results: List<Result>?
)
data class Result(
val category: String?,
val correct_answer: String?,
val difficulty: String?,
val incorrect_answers: List<String>?,
val question: String?,
val type: String?
)
private suspend fun getTestData():Response {
return client.get("https://opentdb.com/api.php?amount=10")
}
此外,如果您想为每个字段设置自己的名称,您可以设置
@SerializedName("Field Name")
每个字段。