Home > Blockchain >  Parse json map in Retrofit
Parse json map in Retrofit

Time:09-30

I have web service that returns json response like this

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

How do i define data class to handle this response?

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

)

CodePudding user response:

That would be something like that:

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

I've been able to parse it with this test:

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

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

Remember to wrap your json with {} for the test

CodePudding user response:

This JSON represent array of objects array.

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