【问题标题】:"IllegalStateException: Expected BEGIN_OBJECT but was STRING" error in JSON fileJSON 文件中的“IllegalStateException:预期 BEGIN_OBJECT 但为 STRING”错误
【发布时间】:2020-07-01 20:13:06
【问题描述】:

我正在制作一个项目(用于 uni 中的编程类),但是当我尝试在 Android Studio 中运行它时,模拟器中会非常短暂地出现警告:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 5 column 21 path $[0].dateOfBirth

这是我的 JSON 文件:

{
  "fighters": [
    {
      "id": 1,
      "name": "Karl",
      "dateOfBirth": "20-03-1975",
      "level": 4,
      "image": "karl.png"
    },
    {
      "id": 2,
      "name": "Geralt",
      "dateOfBirth": "16-08-1964",
      "level": 8,
      "image": "Geralt.png"
    },
    {
      "id": 3,
      "name": "Darrak",
      "dateOfBirth": "25-11-1940",
      "level": 5,
      "image": "Darrak.png"
    },
    {
      "id": 4,
      "name": "Jafar",
      "dateOfBirth": "09-02-1920",
      "level": 9,
      "image": "Jafar.png"
    },
    {
      "id": 5,
      "name": "Cornelius",
      "dateOfBirth": "28-06-1988",
      "level": 2,
      "image": "Cornelius.png"
    },
    {
      "id": 6,
      "name": "Laila",
      "dateOfBirth": "18-10-1998",
      "level": 5,
      "image": "Laila.png"
    },
    {
      "id": 7,
      "name": "Marianne",
      "dateOfBirth": "01-03-1975",
      "level": 7,
      "image": "Marianne.png"
    },
    {
      "id": 8,
      "name": "Petro",
      "dateOfBirth": "10-07-1974",
      "level": 10,
      "image": "Petro.png"
    },
    {
      "id": 9,
      "name": "Ordelia",
      "dateOfBirth": "18-05-1985",
      "level": 5,
      "image": "Ordelia.png"
    },
    {
      "id": 10,
      "name": "Lucina",
      "dateOfBirth": "21-09-1992",
      "level": 9,
      "image": "Lucina.png"
    },
    {
      "id": 11,
      "name": "Hugo",
      "dateOfBirth": "16-07-1938",
      "level": 6,
      "image": "Hugo.png"
    },
    {
      "id": 12,
      "name": "Sildar",
      "dateOfBirth": "19-12-1980",
      "level": 3,
      "image": "Sildar.png"
    },
    {
      "id": 13,
      "name": "Zenok",
      "dateOfBirth": "30-10-1999",
      "level": 1,
      "image": "Zenok.png"
    },
    {
      "id": 14,
      "name": "Violet",
      "dateOfBirth": "02-04-2001",
      "level": 8,
      "image": "Violet.png"
    },
    {
      "id": 15,
      "name": "Tamara",
      "dateOfBirth": "13-06-1963",
      "level": 4,
      "image": "Tamara.png"
    }
  ],
  "encounters": [
    {
      "id": 1,
      "fighterId": 1,
      "amount_of_monsters": 4,
      "difficulty": "Medium"
    },
    {
      "id": 2,
      "fighterId": 5,
      "amount_of_monsters": 1,
      "difficulty": "Easy"
    },
    {
      "id": 3,
      "fighterId": 5,
      "amount_of_monsters": 2,
      "difficulty": "Medium"
    },
    {
      "id": 4,
      "fighterId": 7,
      "amount_of_monsters": 1,
      "difficulty": "Hard"
    },
    {
      "id": 5,
      "fighterId": 11,
      "amount_of_monsters": 7,
      "difficulty": "Medium"
    },
    {
      "id": 6,
      "fighterId": 7,
      "amount_of_monsters": 2,
      "difficulty": "Easy"
    },
    {
      "id": 7,
      "fighterId": 14,
      "amount_of_monsters": 10,
      "difficulty": "Extreme"
    },
    {
      "id": 8,
      "fighterId": 13,
      "amount_of_monsters": 4,
      "difficulty": "Medium"
    },
    {
      "id": 9,
      "fighterId": 7,
      "amount_of_monsters": 5,
      "difficulty": "Hard"
    },
    {
      "id": 10,
      "fighterId": 3,
      "amount_of_monsters": 5,
      "difficulty": "Easy"
    }
  ]
}

第 5 行是“名称”:“Karl”,

我的 dateOfBirth 属性有问题,我不知道为什么,因为对我来说语法看起来是正确的。我尝试重新安装应用程序并重建项目,但没有成功。

这是我第一次在 StackOverflow 上发布问题,如果有不清楚的地方,敬请原谅。 如果有人能提供帮助,我将不胜感激。

编辑(附加信息)

我使用的是 Android Studio 3.6,API 29。

Android Gradle 插件版本:3.5.3

Gradle 版本:5.4.1

我使用 GSON 来解析 JSON

JDK 11

这是我用于 GSON 类的函数:

fun getFighters(): Observable<Array<Fighter>> {
        val observable = Observable.create<Array<Fighter>> { emitter ->
            try {
                var connection = connect("${BASE_URL}/fighters")
                val gson = GsonBuilder().create()
                val fighters = gson.fromJson(
                    InputStreamReader(connection.inputStream),
                    Array<Fighter>::class.java
                )
                for (fighter in fighters) {
                    connection = connect("${BASE_URL}/${fighter.image}")
                    fighter.imageBitmap = BitmapFactory.decodeStream(connection.inputStream)
                }
                emitter.onNext(fighters)
            } catch(e: Exception) {
                emitter.onError(e)
            }
        }
        return observable
    }

BASE_URL 只是一个测试 URL,因为我们要做的任务并不需要实际部署应用程序。 for 循环是在 RecyclerView 列表中显示 Fighters 的图像,因此我使用了位图。此外,这是基本的 Fighter 数据类:

data class Fighter(
    val id: Number,
    val name: String,
    val dateOfBirth: LocalDate,
    val level: Number,
    val image: String,
    var imageBitmap: Bitmap
)

【问题讨论】:

  • 您好 ;-) 感谢您的提问,但可能缺少一些信息来指导其他人。您可能想要添加您正在使用的 JDK 版本、您用来解析 JSON 的库、Android 版本等。所有可能有助于其他人为您提供建设性答案的东西 ;-) 谢谢。
  • @Ethenyl 感谢您的建议!我添加了尽可能多的附加信息。
  • @FunkyYosh 请发布您用于解析此 JSON 的 GSON 类
  • @ReazMurshed 我添加了这个和其他一些东西,谢谢你的建议。
  • 谢谢!请在下面查看我的回答,如果有帮助请告诉我!

标签: android json android-studio illegalstateexception


【解决方案1】:

我认为您用于解析 JSON 的类应修改如下。

data class Fighter(
    val id: Number,
    val name: String,
    val dateOfBirth: String,
    val level: Number,
    val image: String,
    var imageBitmap: Bitmap
)

dateOfBirth 在您的 JSON 中存储为 String,您需要以这种方式获取它。如果您需要将该值转换为 LocalDate 对象,您可以在解析信息后随时执行此操作。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多