在 Retrofit 中解析 json 地图

Parse json map in Retrofit

我的 Web 服务 returns json 响应是这样的

"map": [
        [
            0,
            "A mother of our mother"
        ],
        [
            2,
            "A brother of our father"
        ],
        [
            1,
            "A daughter of our sister"
        ]
    ],

如何定义数据 class 来处理此响应?

data class Map(
   @SerializedName("map")
   val map : <What type/class definition here>

)

应该是这样的:

data class Map(
    @SerializedName("map")
    val map : List<Collection<Any>>
)

我已经能够通过这个测试解析它:

@Test
fun testJson() {
    val myMap = Gson().fromJson(json, Map::class.java)

    myMap.map.forEach { it ->
        println(it)
    }
    assert(myMap != null)
}

记得用 {} 包装您的 json 以进行测试

这个JSON表示对象数组。

data class Map(
  @SerializedName("map")
  val map : List<List<Any?>>
)