Angular 2使用json中的对象数组获取对象

时间:2020-11-29 20:10:44

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 :

我从json得到一个带有一组对象的对象,但是这个类没有填充,所以我得到一个ndefined错误,这里是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:

这是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:

这是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 :

我也尝试将get更改为:

  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}]}

'http:// localhost:8080 / features'= {“features”:[{“id”:“1”,“name”:“feature1”,“description”:“Dit is Feature 1”)的json输出, “keyword”:“Feature”,“failed”:false},{“id”:“2”,“name”:“feature2”,“description”:“Dit is Feature 2”,“keyword”:“Feature” , “失败”:假}]}

1 个解决方案

#1


0  

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.

#1


0  

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.