Android Retrofit - 如何解析 Github Emojis API

Android Retrofit - How to parse Github Emojis API

请检查此 api:https://api.github.com/emojis

这是回复的一部分:

{
  "+1": "https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png?v8",
  "-1": "https://github.githubassets.com/images/icons/emoji/unicode/1f44e.png?v8",
  "100": "https://github.githubassets.com/images/icons/emoji/unicode/1f4af.png?v8",
  "1234": "https://github.githubassets.com/images/icons/emoji/unicode/1f522.png?v8",
  "1st_place_medal": "https://github.githubassets.com/images/icons/emoji/unicode/1f947.png?v8",
  "2nd_place_medal": "https://github.githubassets.com/images/icons/emoji/unicode/1f948.png?v8",
  "3rd_place_medal": "https://github.githubassets.com/images/icons/emoji/unicode/1f949.png?v8",
  "8ball": "https://github.githubassets.com/images/icons/emoji/unicode/1f3b1.png?v8",
  "a": "https://github.githubassets.com/images/icons/emoji/unicode/1f170.png?v8",
  "ab": "https://github.githubassets.com/images/icons/emoji/unicode/1f18e.png?v8",
  "abacus": "https://github.githubassets.com/images/icons/emoji/unicode/1f9ee.png?v8",
  "abc": "https://github.githubassets.com/images/icons/emoji/unicode/1f524.png?v8",
  "abcd": "https://github.githubassets.com/images/icons/emoji/unicode/1f521.png?v8",
}

我想将此回复转换为表情符号列表。

data class Emoji(
    val name: String,
    val url: String,
)

请注意,响应是一个大对象,我需要一个列表。

这就是我实例化 Retrofit 的方式:

val retrofit = Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL)
        .addConverterFactory(MoshiConverterFactory.create())
        .build()

如何实现?

您没有附加 Api 接口,但根据您的描述,您已将 List<Emoji> 放入 api 接口,这将引发 MalformedJSONException

请改用 Map<String, String>,如果您需要列表,请使用 responseMap.map{ Emoji(it.key, it.valie) }