Angular data model2

0

Good morning,

I have the following JSON:

{
    "tempext": [
        {
            "attrName": "tempext",
            "attrValue": "17.054.495",
            "recvTime": "2017-11-26T18:45:00.000Z"
        },
        {
            "attrName": "tempext",
            "attrValue": "17.054.495",
            "recvTime": "2017-11-26T18:45:00.000Z"
        }
 ],
    "tempint": [
        {
            "attrName": "tempint",
            "attrValue": "19.018.230",
            "recvTime": "2017-11-26T18:45:00.000Z"
        },

        {
            "attrName": "tempint",
            "attrValue": "28.634.987",
            "recvTime": "2017-10-24T12:00:00.000Z"
        }
    ]
}

I'm trying to work with him in Angular 2 but I'm not sure how to put the tempint or tempext information in the data model.

Everything I have designed is more basic and in the data model you do not have to make reference to those two objects, it's something simpler.

export class Temp{
  constructor(
    public recvTime: string,
    public attrName: string,
    public attrValue: string
  ){}
}

Greetings and thanks.

EDIT01

I'm trying to use the interface

interface MyData {
tempext: MyElem[];
tempint: MyElem[];
}

interface MyElem {
recvTime: string;
attrName: string;
attrValue: string;
}

but when it comes to assigning the interface to my controller, I'm not sure how to do it.

Before having the two objects and with the class, I did it in the following way: public mostrarfechaline () {

this._invService.getinvfechasensores(this.fecha3,this.fecha4).subscribe(
                  response => {
                  if (!response) {
                    console.log('error al cargar datos');
                  } else {
this.temp = response;

and thus it recovered all the values of the field:

var f0=this.temp.map(tempext => tempext.recvTime);

How can I do this now?

this way I recover the complete JSON of the get in the service:

getinvfecha(fecha1,fecha2){

    return this._http.get(this.url+'getinvfecha/'+fecha1+'/'+fecha2).map(res => res.json());

  }
    
asked by Manolait 12.01.2018 в 17:05
source

2 answers

0

If you just want to define the attributes of a variable whose value you get with JSON.parse () and you're not really going to use the class constructor, I recommend using interfaces, which simply indicate what the object is like:

interface MyData {
  tempext: MyElem[];
  tempint: MyElem[];
}

interface MyElem {
  recvTime: string;
  attrName: string;
  attrValue: string;
}

And when you get the courage, simply do something like:

 let data= JSON.parse(response) as MyData;
    
answered by 12.01.2018 в 17:29
0

The definition of Class is fine, it's like this:

export class Temp{
  constructor(tempext: TempItem[])
  ){}
  constructor(tempint: TempItem[])
  ){}
}
export class TempItem{
  constructor(
    recvTime: string,
    attrName: string,
    attrValue: string
  ){}
}

If you want to use a interface , it should be like this:

interface Temp{
    tempext: TempItem[];
    tempint: TempItem[];
}
export interface TempItem{
    recvTime: string;
    attrName: string;
    attrValue: string;
}

Then to convert your JSON you have an option for each:

Interface:

 this.http.get('...')
     .map(res => <Temp[]>res.json());

Class:

this.http.get('...')
    .map(res => {
      let data = res.json();
      return data.map(d => {
        return new Temp(d);
      });
    });

Difference:

  

The interfaces are only at compile time. This only   allows you to verify that the expected, received data follow a   particular structure. For this, you can send your content to this   interface.

     

Instead the class is present at the execution time (like the   builder function) and you can define more methods in them with the   processing if necessary.

    
answered by 12.01.2018 в 17:38