Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
Java/Kotlin JSON parsing improvement

I consume an API which gives me this type of JSON:

{
  "data": {
    "name": "Start",
    "pid": "1",
    "position": {
      "data": {
        "x": "31",
        "y": "330"
      },
      "metadata": "empty"
    }
  },
  "metadata": "empty"
}

I have created the classes with objects with the same structure as the above JSON. I use the retrofit lib in Android which inside uses GSON for parsing the JSON.

My model classes would be like this:

MResponse.class

public class MResponse {

    @SerializedName("data")
    public User user;

    String metadata;
}

User.class

public class User {

    public String name;

    public String pid;

    @SerializedName("position")
    public PositionData positionData;
}

PositionData.class

public class PositionData {

    @SerializedName("data")
    public Position position;

    public String metadata;
}

Position.class

public class Position {

    public String x;

    public String y;
}

Now this works fine for me. But as you can see for every model I have to create a parent which will have the same structure just changes the child. This fact doubles the classes that I use for my models. I would like to ask if there is a better way to avoid all these classes.

I don't want to use inner classes. I was thinking that the guys that have done the JSON like this must have had a reason why they did it like this and also a way to make the parsing more easier.

Usually I was used to parse this kind of JSON structure:

{
  "data": {
    "name": "Start",
    "pid": "1",
    "position": {
      "x": "31",
      "y": "330"
    }
  }
}

And here it's easier if I would follow the solution above.

EDIT

Also any solution in Kotlin is welcomed

EDIT 2

Solution for Kotlin here

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I don't think your request is possible. If there is a solution, then it would just create intermediate classes that would do pretty much the same as what you're doing.

In Kotlin, I'd do the above like this. Feel free to even put it in one .kt file:

data class MResponse(@SerializedName("data") val user: User,
                     val metadata: String)

data class User(val name: String,
                val pid: String,
                @SerializedName("position") val positionData: PositionData)


data class PositionData(@SerializedName("position") val position: Position,
                        val metadata: String)

data class Position(val x: String, val y: String)
over 4 years ago · Santiago Trujillo Denunciar

0

Well the solution that I was asking for was quite simple in my opinion, I just didn't know it in the beginning.

Based on source code of Retrofit and some other related answers about Java generics this was the answer for my problem:

public class DataResponse<T, R> {

    public T data;

    @SerializedName("meta")
    public R metadata;
}

and this is an example how I can use it:

Observable<DataResponse<User, BaseMeta>> getUser()

For me this was the solution. If there are better solution out there, I am ready to accept them.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Asistente virtual