0

I'm trying to parse a JSON without results. I've a response like following:

{
  "0": {
    "id": "4",
    "nome": "Zero Gravity",
    "id_stop": "0"
  },
  "1": {
    "id": "540",
    "nome": "First Name",
    "id_stop": "111"
  }
}

The problem is that it's not a list with a defined id, but the id is a progressive integer that varies according to the possible answers. How do I do proper parsing? I tried building a response like this:

here my service

@GET("{path}")
suspend fun getResponse(
    @Path(value = "path", encoded = true) path: String,
    @Query(value = "cmd") alias: String? = "cmd",
    @Query(value = "id_where") idWhere: String,
): Response<ArrayList<APIResponse>>

Here APIResponse

data class APIResponse(
    @JsonProperty("id")
    val id: String?,

    @JsonProperty("nome")
    val nome: String?,

    @JsonProperty("id_stop")
    val idStop: String?,
) {
    fun toDomain(): API {
        return API( id, nome, idStop
        )
    }
}

here my repository

suspend fun getAPIResponse(from: String) : API {
        val response = service.getResponse(path = basePath, idWhere = where)
        return response.body()?.map {
           it.toDomain()
        }
    }

This isn't the solution though, because I can't get a complete answer but I always have a single item with all fields null. Should I use a HashMap? how could i solve?

DoctorWho
  • 1,044
  • 11
  • 34

0 Answers0