skip to Main Content

I’m having trouble converting data from an API to a Java object in Kotlin. For example, if there are two pieces of information in the API, I should make a data class like this:

data class Persons(
    val person1: Person,
    val person2: Person
)

But if there are five pieces of information, I should make a data class like this:

data class Persons(
    val person1: Person,
    val person2: Person,
    val person3: Person,
    val person4: Person,
    val person5: Person,
)

I’m not sure how to handle this situation, because the number of pieces of information that can come from the API can change with each request. Sometimes only one piece of information comes, while other times 20 pieces can come at once. What should I do to handle this?

by the way, this is my cute retrofit builder;

    @Provides
    @Singleton
    fun provideRetrofit(): ExampleApi{
        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ExampleApi::class.java)
    }

What should I do now? I think I’m missing something and I need some help. Additionally, I’m providing an example JSON response that might be useful, as my API call is successful but I’m having trouble with the DTO. Here’s the example JSON:

{
    "person1": {
    "name": {
        "name": "hello",
        "surname": "hello",
    },
    "gender": {
        "topRank": 10,
        "age": 10,
    }
},
    "person2": {
    "name": {
        "name": "hello",
        "surname": "hello",
    },
    "gender": {
        "topRank": 10,
        "age": 10,
    }
}
}

2

Answers


  1. Have you tried using Map ?

    class Example(
        val Map<String,Person> response:
    )
    

    and wrap the response in a key

     {
        "result": {
            "person1": {
            "name": {
                "name": "hello",
                "surname": "hello",
            },
            "gender": {
                "topRank": 10,
                "age": 10,
            }
        },
            "person2": {
            "name": {
                "name": "hello",
                "surname": "hello",
            },
            "gender": {
                "topRank": 10,
                "age": 10,
            }
        }
        }
    
    }
    

    the key is to wrap the response with a new object key "response" and cast it using Example class

    Login or Signup to reply.
  2. This problem is difficult to solve, I suggest you talk to the backend and get him to modify to the following format:

    {
    "result": [
        {
            "name": {
                "name": "hello",
                "surname": "hello"
            },
            "gender": {
                "topRank": 10,
                "age": 10
            }
        },
        {
            "name": {
                "name": "hello",
                "surname": "hello"
            },
            "gender": {
                "topRank": 10,
                "age": 10
            }
        }
     ]
    }
    

    Then you can use List to receive it:

    List<Person>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search