简体   繁体   English

带有 Ktor/Kotlinx 的有效 Json 上的 JsonDecodingException

[英]JsonDecodingException on valid Json with Ktor/Kotlinx

Why I get the following error at offset 6 with the following code.为什么使用以下代码在偏移 6 处出现以下错误。 It makes an HTTP Request, gets a Json back, should go through the Json and create the object IMDBInfo.它发出一个 HTTP 请求,返回一个 Json,应该通过 Json 并创建对象 IMDBInfo。 The json is valid, obviously processing, and the data class is as easy as it could be..., but I don't get the point of the error: json 是有效的,显然正在处理,并且数据类尽可能简单......,但我不明白错误的重点:

Error:错误:

Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 6: Expected beginning of the string, but got {
JSON input: {"d":[{"i":{"height":741,"imageUrl":.....
    at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
    ...

Code:代码:

class StreamingAvailability() {
        var IMDBName : String = ""
    
        fun findOriginalTitle(title: String) = runBlocking {
            val client = HttpClient(Apache) {
                install(JsonFeature) {
                    serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
                        prettyPrint = true
                        isLenient = true
                        ignoreUnknownKeys = true
                        coerceInputValues = true
                        allowStructuredMapKeys = true
                    })
                }
            }
            val result : IMDBInfo = client.get {
                url {
                    protocol =  URLProtocol.HTTPS
                    encodedPath = "auto-complete"
                    host = "imdb8.p.rapidapi.com"
                }
                parameter("q", title)
                headers {
                    append(HttpHeaders.Accept, "application/json")
                    append(HttpHeaders.ContentType, ContentType.Application.Json)
                    append(HttpHeaders.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36")
                    append(HttpHeaders.Authorization, API_KEY)
                    append("X-Rapidapi-Key", API_KEY)
                    append("X-Rapidapi-Host", "imdb8.p.rapidapi.com")
                }
            }
    
            println(result.d)
        }
    }

Data Classes:数据类:

import kotlinx.serialization.Serializable

@Serializable data class D(
    val l: String
)
    @Serializable data class IMDBInfo(
    val d: List<D>,
)

JSON which is valid and processed in the response:在响应中有效并处理的 JSON:

{
    "d": [{
        "i": {
            "height": 800,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTMzNDkzMTcyOV5BMl5BanBnXkFtZTcwNDIzMjM2MQ@@._V1_.jpg",
            "width": 550
        },
        "id": "tt1080016",
        "l": "Ice Age: Dawn of the Dinosaurs",
        "q": "feature",
        "rank": 7936,
        "s": "Ray Romano, John Leguizamo",
        "v": [{
            "i": {
                "height": 360,
                "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTMyODMyMDY3MF5BMl5BanBnXkFtZTcwMTg2MTM0Mg@@._V1_.jpg",
                "width": 480
            },
            "id": "vi3380019993",
            "l": "Ice Age: Dawn of the Dinosaurs -- Trailer #2",
            "s": "2:30"
        }, {
            "i": {
                "height": 360,
                "imageUrl": "https://m.media-amazon.com/images/M/MV5BMjFkMjY3NzYtNTkzOS00ZWM4LThhN2MtZTk0MTczMGRjZmNiXkEyXkFqcGdeQXVyNzU1NzE3NTg@._V1_.jpg",
                "width": 480
            },
            "id": "vi64291353",
            "l": "Ice Age: Dawn of the Dinosaurs",
            "s": "0:59"
        }, {
            "i": {
                "height": 360,
                "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTg1NTgwNzg5M15BMl5BanBnXkFtZTgwOTc4NzkxMzE@._V1_.jpg",
                "width": 480
            },
            "id": "vi2023162649",
            "l": "Ice Age: Dawn of the Dinosaurs -- Trailer #1",
            "s": "2:34"
        }],
        "vt": 5,
        "y": 2009
    }, {
        "i": {
            "height": 500,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMjE1NTEwMTEwOF5BMl5BanBnXkFtZTcwMDA2MDQyOQ@@._V1_.jpg",
            "width": 357
        },
        "id": "tt1907779",
        "l": "The Dinosaur Project",
        "q": "feature",
        "rank": 39963,
        "s": "Richard Dillane, Peter Brooke",
        "v": [{
            "i": {
                "height": 480,
                "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTg3ODAxMTg4OF5BMl5BanBnXkFtZTcwNTg0OTI4OA@@._V1_.jpg",
                "width": 640
            },
            "id": "vi3951666969",
            "l": "The Dinosaur Project Trailer",
            "s": "2:11"
        }],
        "vt": 1,
        "y": 2012
    }, {
        "i": {
            "height": 789,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTk0MTI1NTI1MF5BMl5BanBnXkFtZTcwMDg2Mzc4OQ@@._V1_.jpg",
            "width": 603
        },
        "id": "tt2303110",
        "l": "Rise of the Dinosaurs",
        "q": "feature",
        "rank": 46988,
        "s": "Gary Stretch, Corin Nemec",
        "y": 2013
    }, {
        "i": {
            "height": 1285,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMWI4ZjZmYTktOWIxNS00MmMyLTk5YzctNGQ4ZDg3MmIxYmZkXkEyXkFqcGdeQXVyODg1Njg2Njc@._V1_.jpg",
            "width": 900
        },
        "id": "tt7818384",
        "l": "Dino the Dinosaur",
        "q": "TV series",
        "rank": 65868,
        "s": "June Yoon",
        "y": 2016,
        "yr": "2016-2019"
    }, {
        "i": {
            "height": 475,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTU2NDkyODcxM15BMl5BanBnXkFtZTcwNTA0MzQyMQ@@._V1_.jpg",
            "width": 301
        },
        "id": "tt0136639",
        "l": "Extreme Dinosaurs",
        "q": "TV series",
        "rank": 83132,
        "s": "Scott McNeil, Cusse Mankuma",
        "y": 1997,
        "yr": "1997-1997"
    }, {
        "i": {
            "height": 475,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTIzODM4NTYyMV5BMl5BanBnXkFtZTcwODYzMzAzMQ@@._V1_.jpg",
            "width": 253
        },
        "id": "tt0103400",
        "l": "The Dinosaurs!",
        "q": "TV series",
        "rank": 189562,
        "s": "Barbara Feldon, Robert Bakker",
        "y": 1992,
        "yr": "1992-"
    }, {
        "i": {
            "height": 2048,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BNGFlNDczMjMtNmQ1OS00MTJjLTk4NDQtNWU2OGY4Y2M2NDdlXkEyXkFqcGdeQXVyNjE4OTE4OTc@._V1_.jpg",
            "width": 1418
        },
        "id": "tt14162824",
        "l": "The Dinosaur",
        "q": "feature",
        "rank": 211135,
        "s": "Veikko Aaltonen, Rauni Mollberg",
        "y": 2021
    }, {
        "i": {
            "height": 720,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BYjQ3MTU3ZjgtNDA5Zi00N2EwLWExNjctZWJhMzFlMGVjOGJjXkEyXkFqcGdeQXVyNTg0NTkzNTk@._V1_.jpg",
            "width": 1280
        },
        "id": "tt6877360",
        "l": "The Day the Dinosaurs Died",
        "q": "TV movie",
        "rank": 212976,
        "s": "Alice Roberts, Ben Garrod",
        "y": 2017
    }],
    "q": "diedinos",
    "v": 1
}

Just rename the class D to something that has more than 1 symbol.只需将D类重命名为具有 1 个以上符号的名称。 I've created an issue for this really weird behavior.我为这种非常奇怪的行为创建了一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 kotlinx.serialization.json.internal.JsonDecodingException:数组'['的预期开始,但有'EOF' - kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' 为什么我在 kotlinx.serialization 中收到 JsonDecodingException 异常? - Why am I getting a JsonDecodingException exception in kotlinx.serialization? 使用 kotlinx.serialization 在 ktor 服务器中接收 Map - Receive Map in ktor server using kotlinx.serialization ktor Apache 客户端发布请求 - 未找到转换:class kotlinx.coroutines.io.ByteBufferChannel - ktor Apache client post request - No transformation found: class kotlinx.coroutines.io.ByteBufferChannel Kotlinx 部分没有键的 JSON 数组序列化 - Kotlinx serialization of JSON Array partially without keys Ktor Websocket 功能与 ktor 中的 ContentNeogation 功能 (JSON / GSON) - Ktor Websocket feature with ContentNeogation feature (JSON / GSON) in ktor 使用 Kotlinx.Serialization 解析嵌套的 JSON - Parse a nested JSON with Kotlinx.Serialization Kotlinx.Serializer - 创建一个快速的 JSON 发送 - Kotlinx.Serializer - Create a quick JSON to send 在 kotlinx.serialization 中编码/解码 JSON “字符串” - Encode / decode JSON “string” in kotlinx.serialization Ktor:如何验证 JSON 请求? - Ktor: How can I validate JSON request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM