Ethernets
Ethernets

Reputation: 201

How to get the correct data in Retrofit

I have JSON model,

{
  "legends": {
    "all": {
      "Revenant": {
        "ImgAssets": {
          "icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/revenant.png",
          "banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/revenant.jpg"
        }
      },
      "Crypto": {
        "ImgAssets": {
          "icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/crypto.png",
          "banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/crypto.jpg"
        }
      },
      "Horizon": {
        "data": [
          {
            "name": "Season 7 wins",
            "value": 1,
            "key": "wins_season_7",
            "rank": {
              "rankPos": 62637,
              "topPercent": 69.39
            },
            "rankPlatformSpecific": {
              "rankPos": 56257,
              "topPercent": 68.58
            }
          },
          {
            "name": "Special event kills",
            "value": 101,
            "key": "specialEvent_kills",
            "rank": {
              "rankPos": 101058,
              "topPercent": 65.11
            },
            "rankPlatformSpecific": {
              "rankPos": 68184,
              "topPercent": 61.62
            }
          },
          {
            "name": "Special event damage",
            "value": 47004,
            "key": "specialEvent_damage",
            "rank": {
              "rankPos": 96259,
              "topPercent": 58.78
            },
            "rankPlatformSpecific": {
              "rankPos": 66031,
              "topPercent": 55.63
            }
          }
        ],
        "ImgAssets": {
          "icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/horizon.png",
          "banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/horizon.jpg"
        }
      },
      "Wraith": {
        "data": [
          {
            "name": "Kills",
            "value": 1356,
            "key": "kills",
            "rank": {
              "rankPos": 162196,
              "topPercent": 12.69
            },
            "rankPlatformSpecific": {
              "rankPos": 121593,
              "topPercent": 10.29
            }
          },
          {
            "name": "Season 7 kills",
            "value": 233,
            "key": "kills_season_7",
            "rank": {
              "rankPos": 17859,
              "topPercent": 38.15
            },
            "rankPlatformSpecific": {
              "rankPos": 15854,
              "topPercent": 36.65
            }
          },
          {
            "name": "Season 7 wins",
            "value": 13,
            "key": "wins_season_7",
            "rank": {
              "rankPos": 11891,
              "topPercent": 13.97
            },
            "rankPlatformSpecific": {
              "rankPos": 10926,
              "topPercent": 13.47
            }
          },
          {
            "name": "Special event kills",
            "value": 1377,
            "key": "specialEvent_kills",
            "rank": {
              "rankPos": "NOT_CALCULATED_YET",
              "topPercent": "NOT_CALCULATED_YET"
            },
            "rankPlatformSpecific": {
              "rankPos": "NOT_CALCULATED_YET",
              "topPercent": "NOT_CALCULATED_YET"
            }
          },
          {
            "name": "Special event damage",
            "value": 656195,
            "key": "specialEvent_damage",
            "rank": {
              "rankPos": "NOT_CALCULATED_YET",
              "topPercent": "NOT_CALCULATED_YET"
            },
            "rankPlatformSpecific": {
              "rankPos": "NOT_CALCULATED_YET",
              "topPercent": "NOT_CALCULATED_YET"
            }
          },
          {
            "name": "Special event wins",
            "value": 68,
            "key": "specialEvent_wins",
            "rank": {
              "rankPos": "NOT_CALCULATED_YET",
              "topPercent": "NOT_CALCULATED_YET"
            },
            "rankPlatformSpecific": {
              "rankPos": "NOT_CALCULATED_YET",
              "topPercent": "NOT_CALCULATED_YET"
            }
          }
        ],
        "ImgAssets": {
          "icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/wraith.png",
          "banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/wraith.jpg"
        }
      }
    }
  }
}

Then I make a data class:

data class TestHero (@SerializedName("global") val global: PlayerInf,
                     @SerializedName("legends")val legends: AllLegends)

data class  PlayerInf (val name: String, val uid: Long, val avatar: String, val platform: String,
val  level: Int, val toNextLevelPercent: Int, val internalUpdateCount: Int, val bans: BanInf, val rank: RankInf)

data class BanInf (val isActive: Boolean, val remainingSeconds: Int)

data class RankInf (val rankScore: Int, val rankName: String, val rankDiv: Int, val rankImg: String)


data class AllLegends (@SerializedName("all") val all : Revenant, @SerializedName("all")val all2: Horizon)

data class Revenant (val ImgAssets: String)
data class Horizon (val data : ArrayList<Rang0>, val ImgAssets: String)

class Legends(){
    data class Revenant (val ImgAssets: String)
    data class Horizon (val data : ArrayList<Rang0>, val ImgAssets: String)
}

data class Rang (val t0 : Rang0)
data class Rang0 (val name: String, val value: Int, val key: String)

But I get the error:

Unable to create converter for class

Having looked for answers to stackoverflow, I realized that you can not double serialize a class object. But how do you get the data then? I tried to do it separately, but it turned out to get the data of only 1 object example: data class AllLegends (@SerializedName("all") val all : Revenant) help me figure it out please

Upvotes: 0

Views: 82

Answers (1)

cutiko
cutiko

Reputation: 10517

I think that is a map of defined objects, but every legend is undefined

data class Legends(
    val all: Map<String, LegendWrapper> = emptyMap()
)
data class LegendWrapper(
    val data: Lis<PlayerPerformance>? = emptyList(),
    val ImgAssets: ImgAssets? = null

)

So you can define PlayerPerformance and ImgAssets on a class but can't define each "legend" because the keys for each are not static, that is why the map.

And in LegendWrapper you have to define data and ImgAssets as nullables because some "legends" doesn't have them.

This way inside of Legends you don't need to have Ravenant, Crypto, Horizon... and so on.

Upvotes: 1

Related Questions