I am working with a Python REST API and Angular 2 on the frontend. I am facing an issue with parsing GET request responses into objects Answer with date type field. Here is an example of my json:
{
"date_answer": "2015/07/05",
"id": 1,
"text": "some text",
}
Currently, I have this code in my controller:
getAnswers() : Observable<Answer[]> {
return this.http.get(this.answerUrl)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));}
I have tried to address this issue by parsing the Date field in the constructor as shown below:
export class Answer {
id: number;
text: string;
date_answer: Date;
constructor(
id: number,
text: string,
date_string: string
){
this.date_answer = new Date(date_string)
}
}
However, this approach has not been successful. I am wondering if there might be a better way to extract the date from the JSON response as a Date type.