Working with objects in services Angular 4

0

I'm trying to work with this JSON:

{"code":"OK_BEER_100","beers":[{"avaliable":true,"_id":"5a71d971734d1d71dd530b5e","numberbeer":1,"name":"Alhambra 1925 Updated","type":"Baja","origin":"Granada","percent":33.3,"ibus":15,"halfpint":"3.5","fullpint":"5"},{"avaliable":true,"_id":"5a71d9a5734d1d71dd530b6e","numberbeer":2,"name":"Origen","type":"Alta","origin":"Almeria","percent":18,"ibus":5,"halfpint":"4","fullpint":"6.5"},{"avaliable":true,"_id":"5a71d9cd734d1d71dd530b89","numberbeer":3,"name":"Malta Powah","type":"Flipas","origin":"El Congo","percent":99,"ibus":43,"halfpint":"10","fullpint":"12.5"}]} 

I have my Object with two elements and inside Beers contains 3 more objects.

I'm recovering the .json in the service like this:

getBeers(){
   console.log("Consumimos el servicio Beers");
   let headers = new Headers ({'Accept': 'application/json'});
   let options = new RequestOptions ({headers : headers});
   return this._http.get(this.url+'getall', options).map(res => res.json());
 }

For the components like this:

this._beerService.getBeers().subscribe(
      response => {
        if(!response){
          console.log('La respuesta esta vacia');
        }else{
          this.beer = response;
          console.log('mostramos el valor: ' + JSON.stringify(this.beer));
          console.log('Mostramos el this.datos: ' + this.beer);
        }
      },
      error => {
        console.log(<any>error);
      }
    );

My answer is this:

mostramos el valor: {"code":"OK_BEER_100","beers":[{"avaliable":true,"_id":"5a71d971734d1d71dd530b5e","numberbeer":1,"name":"Alhambra 1925 Updated","type":"Baja","origin":"Granada","percent":33.3,"ibus":15,"halfpint":"3.5","fullpint":"5"},{"avaliable":true,"_id":"5a71d9a5734d1d71dd530b6e","numberbeer":2,"name":"Origen","type":"Alta","origin":"Almeria","percent":18,"ibus":5,"halfpint":"4","fullpint":"6.5"},{"avaliable":true,"_id":"5a71d9cd734d1d71dd530b89","numberbeer":3,"name":"Malta Powah","type":"Flipas","origin":"El Congo","percent":99,"ibus":43,"halfpint":"10","fullpint":"12.5"}]} 

The Object

pizarra.component.ts:32 Mostramos el this.datos: [object Object] 

I have created a model with the following information:

export class Beers{
  constructor(
    public avaliable: string,
    public _id: number,
    public numberbeer: number,
    public name: string,
    public type: string,
    public origin: string,
    public percent:  string,
    public ibus: string,
    public halfpint: string,
    public fullpint:string
  ){}
}

But when I try to assign the answer with those data, it tells me that it can not find information.

public beer: Beers;
this.beer = response.beer;

I have to design two models one for the first object and one for the other objects but assigning beers to that object?

for example:

import { Beers } from './beers';
export class Objeto{
  constructor(
    public code: string,
    public beers: Beers
    ){}
}

Everything I find is for JSON at full without the main objects.

The main objective of this is to fill in the html part with an ngfor with the data of the beer

Edit

Then a doubt in the case of having a JSON with several Objects for example like this

{"var_RadiacionExt": [
        {
            "attrName": "var_RadiacionExt",
            "attrValue": "8",
            "recvTime": "2018-02-02T13:57:45.000Z"
        }
    ],
    "var_RadiacionGlobalInt": [
        {
            "attrName": "var_RadiacionGlobalInt",
            "attrValue": "8",
            "recvTime": "2018-02-02T13:57:45.000Z"
        }
    ]}.

What should I assign to each this.miData1 = response.var_RadiacionGlobalInt; and this.miData2 = response.var_RadiacionExt; or in this case to have the same internal structure is not assigned the answer to each of them and becomes complete? this.beer = response;

In this case I'm doing this:

this.miData = response;
let var_RadiacionExt: Inver[] = this.miData.var_RadiacionExt;
let var_RadiacionGlobalInt: Inver[] = this.miData.var_RadiacionGlobalInt;

and so I assign the values:

this.Tempint = var_TempInt[0].attrValue;
this.RHint = var_HumRInt[0].attrValue

Is it correct? that's how it works.

    
asked by Manolait 06.02.2018 в 11:13
source

0 answers