我有这个界面
export interface Student {
cf: string,
firstName: string,
lastName: string,
dateOfBirth: Date,
description?: string,
enrollmentDate?: Date
}
我想用http get request填充一组学生,它为每个学生返回以下 json
{cf: "blablabla", first_name: "Mario", last_name: "Rossi", date_of_birth: "1998-01-24", enrollment_date: "2019-03-20" },
如您所见,该接口的名称与响应不同(firstName而不是first_name),因此当我将学生的姓名打印到控制台时,我得到undefined。
这是我从中获取数据的服务功能
getStudents(): Observable<Student[]> {
return this.httpClient.get<Student[]>(this.studentsUrl, this.baseService.httpOptions);
}
这是我的学生组件
export class StudentsComponent implements OnInit {
students: Student[];
childIcon = faChild;
plusIcon = faPlus;
private _newStudent: boolean = false;
constructor(private studentsService: StudentsService) { }
ngOnInit(): void {
this.studentsService.getStudents().subscribe(
(result: Student[]) => {
this.students = result;
this.students.forEach(student => console.log(student));
},
error => console.log(error)
)
}
}
有没有办法将 json 响应转换为我的学生界面?关于堆栈溢出的几个答案建议map是方法,但我不明白如何使用该运算符 alog 和subscribe