如何在Kotlin中使用Retrofit从JSON解析对象的不确定列表

时间:2019-10-27 17:12:21

标签: android json kotlin retrofit2

我收到以下格式的JSON响应:

"game" : {},
"datetime": {},
"status": {},
"teams": {},
"players" : {
      "ID8470607" : {
        "id" : 8470607,
        "fullName" : "Brent Seabrook",
        "link" : "/api/v1/people/8470607",
        "firstName" : "Brent",
        "lastName" : "Seabrook",
        "primaryNumber" : "7",
        "birthDate" : "1985-04-20",
        "currentAge" : 34,
        "birthCity" : "Richmond",
        "birthStateProvince" : "BC",
        "birthCountry" : "CAN",
        "nationality" : "CAN",
        "height" : "6' 3\"",
        "weight" : 220,
        "active" : true,
        "alternateCaptain" : true,
        "captain" : false,
        "rookie" : false,
        "shootsCatches" : "R",
        "rosterStatus" : "Y",
        "currentTeam" : {
          "id" : 16,
          "name" : "Chicago Blackhawks",
          "link" : "/api/v1/teams/16",
          "triCode" : "CHI"
        },
        "primaryPosition" : {
          "code" : "D",
          "name" : "Defenseman",
          "type" : "Defenseman",
          "abbreviation" : "D"
        }
      },
      "ID8473533" : {
        "id" : 8473533,
        "fullName" : "Jordan Staal",
        "link" : "/api/v1/people/8473533",
        "firstName" : "Jordan",
        "lastName" : "Staal",
        "primaryNumber" : "11",
        "birthDate" : "1988-09-10",
        "currentAge" : 31,
        "birthCity" : "Thunder Bay",
        "birthStateProvince" : "ON",
        "birthCountry" : "CAN",
        "nationality" : "CAN",
        "height" : "6' 4\"",
        "weight" : 220,
        "active" : true,
        "alternateCaptain" : false,
        "captain" : true,
        "rookie" : false,
        "shootsCatches" : "L",
        "rosterStatus" : "Y",
        "currentTeam" : {
          "id" : 12,
          "name" : "Carolina Hurricanes",
          "link" : "/api/v1/teams/12",
          "triCode" : "CAR"
        },
        "primaryPosition" : {
          "code" : "C",
          "name" : "Center",
          "type" : "Forward",
          "abbreviation" : "C"
        }
      },

      etc.
},
"venue": {}

给定游戏的玩家列表是未知的(有些游戏是40,有时是42,它会改变)。如何将这些数据表示为Kotlin类?我尝试将其表示为ArrayList,但是在对其进行解析时出现错误Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 111 column 18 path $.gameData.players

我知道这意味着它期待一个数组(我定义的数组),而是获得了JSON对象ID8470607,但是由于ID一直都是不同的,并且播放器列表的长度每次都会不同,我无法像其他班级那样在字段中进行硬编码。那么我应该如何将其表示为数据类呢?

这是我目前拥有的:

data class GameData (
    val game: GameInfo,
    val datetime: DateTime,
    val status: GameStatus,
    val teams: GameTeams,
    val players: ArrayList<Player>,
    val venue: GameVenue
)

谢谢。

1 个答案:

答案 0 :(得分:1)

问题是您没有收到列表,示例中定义的destroyed() { Event.$off('category.save'); Event.$off('category.exit'); } 是地图。如果您想解析上面显示的示例,则GameData类需要如下所示:

players

这样,您将拥有一个玩家地图(对于此特定示例,您将必须按键data class GameData ( val game: GameInfo, val datetime: DateTime, val status: GameStatus, val teams: GameTeams, val players: Map<String,Player>, val venue: GameVenue ) ID8470607