Problems listing a JSONArray in AndroidStudio

1

I have a json that gives me this result:

{
   "error": false,
   "fatigas": [
      {
         "fatServ_fecha": "2018-01-07",
         "fatServ_HoraIn": "10:13:00",
         "fatServ_HoraOut": null,
         "turn_cod": "3",
         "are_cod": "62",
         "fatServ_cerrar": "D",
         "fatServ_imprimir": "D",
         "fatServ_validacion": "D"
      }
   ]
}

and here is the code where I printed it, the problem is that it does not enter the for

    protected void onPostExecute(String s) {
        super.onPostExecute(s);
            ((Activity) getContext()).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if(jsonObject!=null){
                            items = new ArrayList<>();
                            JSONArray resultados = jsonObject.getJSONArray("respuesta");
                            for (int i = 0; i < resultados.length(); i++) {
                                JSONObject fatigas = resultados.getJSONObject(i);
                                items.add(new Fatigas(fatigas.getString("fatServ_fecha")+"",
                                        fatigas.getString("fatServ_HoraIn")+"",
                                        fatigas.getString("fatServ_HoraOut")+"",
                                        fatigas.getString("turn_cod")+"",
                                        fatigas.getString("are_cod")+"",
                                        fatigas.getString("fatServ_cerrar"),
                                        fatigas.getString("fatServ_imprimir"),
                                        fatigas.getString("fatServ_validacion")));
                            }
                        } else {
                            Toast.makeText(getContext(),"No hay fatigas",Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    FatigasAdapter adapter = new FatigasAdapter(items);
                    rv_listado.setAdapter(adapter);
                    rv_listado.setLayoutManager(new GridLayoutManager(getContext(),1));
                }
            });
        }
    
asked by JUAN PABLO SANGAR 06.02.2018 в 21:18
source

1 answer

0

It does not enter because there is no Array called "answer"

JSONArray resultados = jsonObject.getJSONArray("respuesta");

Actually you have an object that inside has an Array called "fatigue", this is where you must obtain the objects (in the case of the example that shows only one):

...
...
     JSONArray resultados = jsonObject.getJSONArray("fatigas");
                                for (int i = 0; i < resultados.length(); i++) {
                                    JSONObject fatigas = resultados.getJSONObject(i);
                                    items.add(new Fatigas(fatigas.getString("fatServ_fecha")+"",
                                            fatigas.getString("fatServ_HoraIn")+"",
                                            fatigas.getString("fatServ_HoraOut")+"",
                                            fatigas.getString("turn_cod")+"",
                                            fatigas.getString("are_cod")+"",
                                            fatigas.getString("fatServ_cerrar"),
                                            fatigas.getString("fatServ_imprimir"),
                                            fatigas.getString("fatServ_validacion")));
                                }
...
...

I suggest you check:

How to read a file Json on android?

    
answered by 06.02.2018 в 21:39