Read Json ionic 3

0

I need your help on how to read or how to process data from a JSON (Object) that comes from a Java WS. What I've done is use HTTP from IONIC; the server responds correctly My question is how to read this Object (jSON) in IOnic.

My WS returns a List<Empleado>

   @GET
    @Path("empleados")
    @Produces("application/json")
    public List<Empleado> checkTurnos() {
        List<Empleado>emp=daoM.getEmpleadosPlantaMobil();
        return emp;
    }

The class used is this:

    private Integer id_empleado;
    private String nombres;
    private String identificacion;
    private String cargo;
    private Integer cod_reloj;
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name="empleado", referencedColumnName="id_empleado")
    private List<Timbres> timbres=new ArrayList<>();
     @OneToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "id_ruta_fabricacion")
    private Ruta_fabricacion ruta_fabricacion;
     @OneToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "id_turno")
    private Turno turno;
//getters & setters

Here is where I do not know how to read the json

   this.http2.get('url',{},{}).then (data => {
    console.log("STADO "+data.status);
    console.log("DATA "+data.data); // datos del servidor
    console.log("HEADERSSS "+data.headers);    

    }, error => {
      console.log(error);
    })

I must say that the WS is tested and it returns something like:

UPDATE

walking with a for (MY PROBLEM) how I deal with this data, that is if I want to take turns, routes, etc:

for(let k in data.data){
      console.log("> "+k+" "+data.data);
    }

In the console you can see:

    
asked by Maicoly Guerrero 12.06.2018 в 16:01
source

3 answers

1

The problem was that I was just receiving a String and I just needed to do JSON.parse . Thanks @JackNavaRow!

this.empleados=JSON.parse(data.data);
    for(let k in this.empleados){ 
      console.log(this.empleados[k].nombres);
      console.log(this.empleados[k].turno.descripcion);
    }
    
answered by 12.06.2018 в 19:09
0

I think the for you are looking for is:

for (let empleado of data.data) {
  console.log("empleado1", empleado);
  console.log("id del empleado1", empleado.id_empleado);
  console.log("id de la ruta de fabricacion", empleado.ruta_fabricacion.id_ruta_fabricacion);
  console.log("id del turno", empleado.turno.id_turno);
}

Try to see

Greetings

PS: Check the documentation iterators and generators because you are taking the index and not the array object p>     

answered by 12.06.2018 в 16:38
0

The answer you get is a string you need to parcel it with the JSON instruction.parse I leave you a link link also check the format of your json I can not see it well but check it here link depending how you configure the answer you may have to walk more at once.

    
answered by 12.06.2018 в 19:38