我想在JSON响应中循环主题数组
JSON响应:
{
"$id": "1",
"Council_ID": 116,
"number": "67",
"subjects": [
{
"$id": "2",
"subjectCode": "67",
"type": 4,
},
{
"$id": "3",
"subjectCode": "67",
"type": 4,
}
]
}
model.component.ts:
this.dataStorageService.storeSubjects(newSubject, this.meetingID)
.subscribe(
response => {
this.dataStorageService.getCouncilId(this.meetingID).subscribe(response => {
this.subjectsWithID = response.json();
this.sublength = this.subjectsWithID.subjects.length;
console.log(this.sublength);
for(let i = 0 ; i <= this.sublength; i++){
this.typee = this.subjectsWithID.subjects[i].type;
this.dataStorageService.getSubjectTypeId(this.typee).subscribe(response =>{
this.namesOfSub = Array.of( response.json());
console.log(this.namesOfSub)
});
}
// console.log(this.typee, 'bla bla');
});
this.addSubjectForm.reset();
this.router.navigate(['/model']);
return true;
这给了我错误: - 财产&#39;科目&#39;类型&#39;任何[]&#39;都不存在。有什么建议摆脱这个错误?
答案 0 :(得分:1)
确定使用httpClient,你无法获取属性this.subjectsWithID.subjects,但是使用http是可能的,这是因为httpClinet解析结果时它确实知道对象的共享,你必须给出一个类型对于您的结果,或者您可以访问您的财产:
this.subjectsWithID['subjects']
或者您可以使用interface指定类型:
return this.http.get<myObject>(...url)
interface Sidebar {
id: number;
Council_ID: number,
number: number,
subjects: [...
}