Convert JsonArray to JAVA POJO android

0

With retrofit I use a JsonArray, I want to get elements from the Json Objects list

this is my Json

{
"Codigo": 0,
"FechaHora": "2017-07-11T11:23:11",
"Mensaje": "Ok",
"Data": [
    [
        {
            "Id": 1,
            "Nombre": "Humanos",
            "Estado": 1
        }
    ],
    [
        {
            "Id": 1,
            "TDId": 1,
            "GOId": 1,
            "ImgId": null,
            "Nombre": "Hola",
            "Estado": 1
        }
    ]
]
}

I want to get the Data Elements [] this is my Retrofit method

public void sincronizar() {

    fecha = "1990-01-01T00:00:00";
    final Object lista[] = new Object[1];
    lista[0] = fecha;
    parametroApi.setDatoG(lista);

    PedroApi service = retrofit.create(PedroApi.class);
    Call<Respuesta<JsonArray>> call = service.sincronizar(parametroApi);
    call.enqueue(new Callback<Respuesta<JsonArray>>() {
        @Override
        public void onResponse(Call<Respuesta<JsonArray>> call, Response<Respuesta<JsonArray>> response) {
            if (response.isSuccessful()) {
                try {
                    Respuesta<JsonArray> respuesta = response.body();
                    if (respuesta.respuestaExitosa() == true) {
                        Log.e(TAG, "Respuesta Exitosa" + respuesta.toString());
                        String fechas = respuesta.getFechaHora();


                        //Try
                        //Insertar en BD
                        //Actualizar Token = fecha
                        //Cath
                        //NO dio


                    } else if (respuesta.respuestaExitosa() == false) {
                        Log.e(TAG, "Respuesta NO Exitosa" + respuesta.toString());

                    }

                } catch (Exception e) {
                    Log.d(TAG, "Ocurio una Exepcion" + e.getMessage());
                    e.printStackTrace();

                }
            }
        }

        @Override
        public void onFailure(Call<Respuesta<JsonArray>> call, Throwable t) {
            Log.d(TAG, "Falla el consumo : " + t.getMessage().toString());
            PopUp("Errorl al conectarse con el Servidor Por favor intetalo de nuevo");


        }
    });

}

this my Service

 @POST("encuesta/sincronizar")
Call<Respuesta<JsonArray>> sincronizar(@Body ParametroApi<Object[]> parametroApi);

How can I walk the Data JsonObjects []?

    
asked by Marcelo Casanovas 12.07.2017 в 05:05
source

1 answer

0

Problem

You receive a JSONArray instead of a JSONObject .

You want to get the JSONArray of the key property 'data' of the JSONObject, but right now as you describe it is impossible since you do not receive the content of the 'data' property, but the entire JSONObject and you are indicating to retrofit to try turn it into a JSONArray.

Solution 1

Get a JSONObject instead of a JSONArray and you can get your property data data like this:

JSONObject json = res.body();
JSONArray data = json.getJSONArray("data");

Solution 2

Receive from the API directly the array of the property data if you only need that data.

JSONArray data = res.body();

Solution 3

You can create a wrapper or model that represents what you receive, a class. Receive this as a retrofit response and the array data of the data property which is also another wrapper.

class MiRespuesta {
    int Codigo;
    String FechaHora;
    String message;
    List<MiDato> misDatos;
}

class MiDato {
    int id;
    int TDId;
    int GOId;
    Integer ImgId;
    String Nombre;
    String Estado;
}

MiRespuesta mr = res.body();
List<MiDato> datos = mr.misDatos;
    
answered by 12.07.2017 в 09:11