【问题标题】:How to parse indefinite list of objects from JSON with Retrofit in Kotlin如何在 Kotlin 中使用 Retrofit 解析来自 JSON 的无限对象列表
【发布时间】:2019-10-27 17:12:21
【问题描述】:

我收到以下格式的 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
)

提前致谢。

【问题讨论】:

    标签: android json kotlin retrofit2


    【解决方案1】:

    问题是您没有收到列表,示例中定义的players 是一个地图。如果您想解析上面显示的示例,您的 GameData 类需要如下所示:

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

    这样你就可以得到一张玩家地图(对于这个特殊的例子,你必须输入 ID8470607ID8473533

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2019-07-30
      • 2016-08-07
      • 2021-02-03
      • 2021-08-24
      • 2015-09-12
      • 1970-01-01
      • 2016-01-01
      相关资源
      最近更新 更多