Perhaps such a question was asked, but I did not find ((. Help to create a model for this type of json, I get the answer in the form of an object, I would disassemble the array without any problems.

{ "0": { "id": "1", "name": "Антон", "birthday": { "year": "1978", "month": "03", "day": "05", "zodiac": { "name": "Рыбы" } } }, "1": { "id": "2", "name": "Наталия", "birthday": { "year": "1973", "month": "05", "day": "23", "zodiac": { "name": "Близнецы" } } }, "2": { "id": "3", "name": "Света", "birthday": { "year": "1991", "month": "04", "day": "14", "zodiac": { "name": "Овен" } } }, "execution_time": 0.1185 } 

enter image description here

  • Jackson or Gson? - katso
  • I apologize not clarified, Gson - dev.android
  • "0", "1", "2" are the keys? Or they can change? - Timur Mukhortov
  • They increase by +1 as needed, they can reach 100 and 200, that's why I can't figure it out - dev.android

1 answer 1

I suppose you already have a certain class Person .

Write your deserializer

 class PersonsDeserializer : JsonDeserializer<List<Person>> { override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): List<Person> { val persons = ArrayList<Person>() for (entry in json.asJsonObject.entrySet()) { try { persons.add(context.deserialize<Person>(entry.value, Person::class.java)) } catch (ignore: JsonParseException) { } } return persons } } 

To apply

 @JsonAdapter(PersonsDeserializer.class) data class PersonsResponse : ArrayList<Person> 

It should work)

Kotlin code

  • I'm not writing on Kotlin, and the lower class either, I posted a screenshot for better visibility - dev.android
  • @ dev.android Understand what is happening here is easy. The bottom class needs to be got and inherit from ArrayList. - katso
  • The most important thing for me is to understand how to create a model correctly, if not difficult to please, I will be very grateful - dev.android
  • The bottom class is the @JsonAdapter(PersonsDeserializer.class) class PersonsResponse extends ArrayList<Person> model @JsonAdapter(PersonsDeserializer.class) class PersonsResponse extends ArrayList<Person> - katso
  • Did not help ((Maybe I did something wrong - dev.android