请协助我处理从rest api调用中获得的JSON响应,我提供了用于获取数据的函数和返回的json响应。响应包含一个包含多个条目的数组,完整的响应可以在此处找到https://kayaposoft.com/enrico/json/v2.0/?action=getHolidaysForYear&year=2019&country=za&holidayType=public_holiday
我正在使用Spring Boot后端调用外部rest api,如何在有角度的前端使用JSON响应。
getData(year: string, country: string): void {
this.http.get("http://localhost:8080/holiday/byYear/" + this.year + "/" + this.country).subscribe(data => {
console.log(data.json());
});
}
[
{
"date": {
"day": 1,
"month": 1,
"year": 2019,
"dayOfWeek": 2
},
"name": [
{
"lang": "en",
"text": "New Year's Day"
}
],
"holidayType": "public_holiday"
}
]
答案 0 :(得分:-1)
您应该定义期望从api接收的模型:
export class holiday {
date: {
day : number,
month: number,
year: number,
dayOfWeek: number
}
name: {
lang: string,
text: string
}[]
holidayType: string
}
修改您的方法以返回假日数组,如下所示:
getData(year: string, country: string): holiday[] {
const url = `"http://localhost:8080/holiday/byYear/" + this.year + "/" + this.country`;
return this.http.get(url).catch(res => console.log(res));
}
并使用您的假期清单:
holiday[] response = getData('2019', 'us');
希望这会有所帮助