Android data class structure for unlabelled API response

Viewed 21

An API is returning responses without any outlying data fields, but instead only as an array object like this:

[{"name":"John Smith", "age":"44", "address":"1 Main Street, Anywhere, USA"}, {"name":"Jane Taylor", "age":"22", "address":"10 Suburbia Lane, Sometown, USA"},{"name":"Simon Jones", "age":"36", "address":"33 City Boulevard, Midvalley, USA"}]

My response data classes usually include labels, like this:

data class MyResponseClass (
  val status: String? = null,
  val description: String? = null
)

How should I structure a response class that does not include labels?

TIA.


UPDATE:

Following Ivo's answer (thank you, btw), how could such a class inherit a base class?

open class MyBaseReponseClass() : Serializable {
   val status: String? = null
   val description: String? = null
}

data class MyResponseClass (
  val name: String? = null,
  val age: String? = null,
  val address: String? = null
) : MyBaseReponseClass()

The name, age, and address are in an array, but not status and description.

Thanks again!

1 Answers

Not sure what you mean with not using labels. it clearly has name, age and address so it will be

data class MyResponseClass (
  val name: String? = null,
  val age: String? = null
  val address: String? = null
)

And where you indicate the type of the response you say it's a List<MyResponseClass>

Related