Angular 2 从 json 中获取包含对象数组的对象
Angular 2 get object with an array of objects from json
我从 json 得到了一个包含对象数组的对象,但是 class 没有填充好所以我得到一个定义错误这里是 get :
getFeatures() {
return this.http.get('http://localhost:8080/features')
.map((response: Response) => response.json())
.map(({features,Feature}) => new Features(features,Feature));
}
在这里,我尝试从 class
中获取 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();
});
}
这里是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;
}
这里是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;
}
}
我还尝试将 get 更改为:
getFeatures() {
return this.http.get('http://localhost:8080/features')
.map((response: Response) => response.json())
.map(({features,Feature[]}) => new Features(features,Feature[]));
}
但它说:
, expected
'http://localhost:8080/features'的json输出=
{"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}]}
我的错在于我把关键特征当成了一个真正的字符串现在我得到了:
getFeatures() {
return this.http.get('http://localhost:8080/features')
.map((response: Response) => response.json())
.map(({features = [Feature]}) => new Features(features));
}
这有效。
我从 json 得到了一个包含对象数组的对象,但是 class 没有填充好所以我得到一个定义错误这里是 get :
getFeatures() {
return this.http.get('http://localhost:8080/features')
.map((response: Response) => response.json())
.map(({features,Feature}) => new Features(features,Feature));
}
在这里,我尝试从 class
中获取 classpublic 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();
});
}
这里是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;
}
这里是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;
}
}
我还尝试将 get 更改为:
getFeatures() {
return this.http.get('http://localhost:8080/features')
.map((response: Response) => response.json())
.map(({features,Feature[]}) => new Features(features,Feature[]));
}
但它说:
, expected
'http://localhost:8080/features'的json输出=
{"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}]}
我的错在于我把关键特征当成了一个真正的字符串现在我得到了:
getFeatures() {
return this.http.get('http://localhost:8080/features')
.map((response: Response) => response.json())
.map(({features = [Feature]}) => new Features(features));
}
这有效。