0

为什么使用以下代码在偏移量 6 处出现以下错误。它发出一个 HTTP 请求,返回一个 Json,应该通过 Json 并创建对象 IMDBInfo。json 是有效的,显然正在处理,并且数据类尽可能简单......,但我不明白错误的重点:

错误:

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)
    ...

代码:

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)
        }
    }

数据类:

import kotlinx.serialization.Serializable

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

在响应中有效并处理的 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
}
4

1 回答 1

1

只需将类重命名为D具有超过 1 个符号的东西。我为这种非常奇怪的行为创建了一个问题。

于 2021-10-14T12:54:18.663 回答