“ IllegalStateException:JSON文件中应为BEGIN_OBJECT,但为STRING”错误

问题描述 投票:1回答:1

我正在制作一个项目(用于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列表中显示战斗机的图像,因此我使用了位图。此外,这是基本的Fighter数据类:

data class Fighter(
    val id: Number,
    val name: String,
    val dateOfBirth: LocalDate,
    val level: Number,
    val image: String,
    var imageBitmap: Bitmap
)
android json android-studio illegalstateexception
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.