android - 数组的预期开始 '[' ,但有 'EOF' 而不是

标签 android json kotlin serialization

我正在尝试从 API 获取数据。我的应用程序崩溃了,它给了我这个错误:

kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead JSON input: { "generated_at": "2022-05-20T10:29:36+00:00", "summaries": [] }

我搜索了一个解决方案并找到了一个可以纠正 json 响应中的任何错误的网站,但不幸的是它没有用。

这是我的 json 响应(不是整个响应,只有一项):

{
"generated_at": "2022-05-20T09:40:26+00:00",
"schedules": [
    {
        "sport_event": {
            "id": "sr:sport_event:27751052",
            "start_time": "2021-08-13T19:00:00+00:00",
            "start_time_confirmed": true,
            "sport_event_context": {
                "sport": {
                    "id": "sr:sport:1",
                    "name": "Soccer"
                },
                "category": {
                    "id": "sr:category:1",
                    "name": "England",
                    "country_code": "ENG"
                },
                "competition": {
                    "id": "sr:competition:17",
                    "name": "Premier League",
                    "gender": "men"
                },
                "season": {
                    "id": "sr:season:83706",
                    "name": "Premier League 21\/22",
                    "start_date": "2021-08-13",
                    "end_date": "2022-05-22",
                    "year": "21\/22",
                    "competition_id": "sr:competition:17"
                },
                "stage": {
                    "order": 1,
                    "type": "league",
                    "phase": "regular season",
                    "start_date": "2021-08-13",
                    "end_date": "2022-05-22",
                    "year": "21\/22"
                },
                "round": {
                    "number": 1
                },
                "groups": [
                    {
                        "id": "sr:league:56620",
                        "name": "Premier League 21\/22"
                    }
                ]
            },
            "coverage": {
                "type": "sport_event",
                "sport_event_properties": {
                    "lineups": true,
                    "extended_player_stats": true,
                    "extended_team_stats": true,
                    "lineups_availability": "pre",
                    "ballspotting": true,
                    "commentary": true,
                    "fun_facts": true,
                    "goal_scorers": true,
                    "scores": "live",
                    "game_clock": true,
                    "deeper_play_by_play": true,
                    "deeper_player_stats": true,
                    "deeper_team_stats": true,
                    "basic_play_by_play": true,
                    "basic_player_stats": true,
                    "basic_team_stats": true
                }
            },
            "competitors": [
                {
                    "id": "sr:competitor:50",
                    "name": "Brentford FC",
                    "country": "England",
                    "country_code": "ENG",
                    "abbreviation": "BRE",
                    "qualifier": "home",
                    "gender": "male"
                },
                {
                    "id": "sr:competitor:42",
                    "name": "Arsenal FC",
                    "country": "England",
                    "country_code": "ENG",
                    "abbreviation": "ARS",
                    "qualifier": "away",
                    "gender": "male"
                }
            ],
            "venue": {
                "id": "sr:venue:53349",
                "name": "Brentford Community Stadium",
                "capacity": 17250,
                "city_name": "London",
                "country_name": "England",
                "map_coordinates": "51.4907295,-0.2891696",
                "country_code": "ENG"
            }
        },
        "sport_event_status": {
            "status": "closed",
            "match_status": "ended",
            "home_score": 2,
            "away_score": 0,
            "winner_id": "sr:competitor:50",
            "period_scores": [
                {
                    "home_score": 1,
                    "away_score": 0,
                    "type": "regular_period",
                    "number": 1
                },
                {
                    "home_score": 1,
                    "away_score": 0,
                    "type": "regular_period",
                    "number": 2
                }
            ]
        }
    }]}

这是一些数据类:

    @Serializable
data class Schedules(
    @SerialName("generated_at")
    val generatedAt: String,
    @SerialName("schedules")
    val schedules: List<Schedule>
)



    @Serializable
data class Schedule(
    @SerialName("sport_event")
    val sportEvent: SportEvent,
    @SerialName("sport_event_status")
    val sportEventStatus: SportEventStatus
)



@Serializable
data class SportEvent(
    @SerialName("competitors")
    val competitors: List<Competitor>,
    @SerialName("coverage")
    val coverage: Coverage,
    @SerialName("id")
    val id: String,
    @SerialName("sport_event_context")
    val sportEventContext: SportEventContext,
    @SerialName("start_time")
    val startTime: String,
    @SerialName("start_time_confirmed")
    val startTimeConfirmed: Boolean,
    @SerialName("venue")
    val venue: Venue
)

日志:

E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.a356scoresclone, PID: 19000
kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead
JSON input: {
    "generated_at": "2022-05-20T10:47:43+00:00",
    "summaries": []
}
    at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
    at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
    at kotlinx.serialization.json.internal.JsonLexer.fail(JsonLexer.kt:493)
    at kotlinx.serialization.json.internal.JsonLexer.fail(JsonLexer.kt:215)
    at kotlinx.serialization.json.internal.JsonLexer.unexpectedToken(JsonLexer.kt:198)
    at kotlinx.serialization.json.internal.JsonLexer.consumeNextToken(JsonLexer.kt:188)
    at kotlinx.serialization.json.internal.StreamingJsonDecoder.beginStructure(StreamingJsonDecoder.kt:37)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:29)
    at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
    at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:63)
    at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:32)
    at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100)
    at com.jakewharton.retrofit2.converter.kotlinx.serialization.Serializer$FromString.fromResponseBody(Serializer.kt:30)
    at com.jakewharton.retrofit2.converter.kotlinx.serialization.DeserializationStrategyConverter.convert(DeserializationStrategyConverter.kt:11)
    at com.jakewharton.retrofit2.converter.kotlinx.serialization.DeserializationStrategyConverter.convert(DeserializationStrategyConverter.kt:7)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:520)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
    Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@34fb196, Dispatchers.IO]

编辑: 我刚刚意识到我有错误的端点。所以我没有写“schedules.json”,而是写了“summaries.json”。但我仍然有类似的错误:

    kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead
JSON input: .....match_status": "not_started"
            }
        }
    ]
}

最佳答案

你已经在日志中打印了你的 JSON,它被传递给解析器

{
    "generated_at": "2022-05-20T10:47:43+00:00",
    "summaries": []
}

你的解析器期待“schedules”数组(甚至是空的),但没有找到这样的键(没有数组,没有对象,没有值)

所以实际上您的响应看起来不像发布的 JSON(有效)

关于android - 数组的预期开始 '[' ,但有 'EOF' 而不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72317598/

相关文章:

javascript - 如何将现有数据读入 Quill JS

android - RxJava2- Completable永无止境

jquery - 使用phonegap (cordova 1.6.0) 登录Facebook 不适用于Android

android - ADB 无法在 Ubuntu 18.04 上检测到 Android 设备

php - 检查值在 PHP 中的 JSON 对象数组中的位置

json - 检索并使用Volley错误HTTP响应代码和开关大小写来处理错误

Android Jetpack 在 Box 中组成部分边框

kotlin - 在列表中查找子序列

android - 在 Android 的 Done Keypress 上隐藏软键盘?

java - Android 应用程序中出现此错误的原因是什么 - channel 已不可恢复地损坏并将被处置