简体   繁体   中英

Angular 2 get object with an array of objects from json

I get an object with an array of objects from json but the class is not filled wel so i get an ndefined error here is the get :

  getFeatures()  {
    return this.http.get('http://localhost:8080/features')
      .map((response: Response) => response.json())
      .map(({features,Feature}) => new Features(features,Feature));
  }

Here I try to get the class out of the class

public getFeatures(){
    this.featureService.getFeatures().subscribe(res => {
    this.featureArray = res.getFeatureArray();
    console.log("Uitkomst: "+ res);
    console.log("Uitkomst array: "+ res.getFeature(0).getID());
    //this.tekst = this.featureArray[0].getID();
    });

}

Here is features.ts:

import {Feature} from "./Feature";
export class Features {

  constructor(public featuress: string , public feature : Feature[]){}

  public getFeature(i : number): Feature{
    return this.feature[i];
  }
  public getFeatureArray(): Feature[]{
    return this.feature;
  }

Here is feature.ts:

export class Feature{

  constructor(public id : string, public name : string, public desciption : string, public keyword : string, public failedTest : boolean){}

  public getID(){
    return this.id;
  }

}

I also tried to change the get to :

  getFeatures()  {
    return this.http.get('http://localhost:8080/features')
      .map((response: Response) => response.json())
      .map(({features,Feature[]}) => new Features(features,Feature[]));
  }

But it says:

, expected

The json output of ' http://localhost:8080/features ' = {"features":[{"id":"1","name":"feature1","description":"Dit is Feature 1","keyword":"Feature","failed":false},{"id":"2","name":"feature2","description":"Dit is Feature 2","keyword":"Feature","failed":false}]}

My fault lay in that I took the key features as an real string now I got:

getFeatures()  {
    return this.http.get('http://localhost:8080/features')
      .map((response: Response) => response.json())
      .map(({features = [Feature]}) => new Features(features));
  }

This works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM