2
我正在使用Angular 4的新HttpModule從REST服務中檢索數據。問題是,數據包含Date類型的屬性。我見過this thread但是當我嘗試在線程建議的解決方案,我得到以下錯誤:res.json is not a function
Angular 4.4來自REST服務的日期反序列化
這裏是我的代碼:
getMarkets(): Observable<Christmasmarket[]> {
let markets: Christmasmarket[];
return this.http.get(this.apiUrl).map(this.extractData);
}
private extractData(res: Response) {
let body = res.json().data;
console.log(body);
body.forEach((d) => {
d.start = new Date(d.start)
d.end = new Date(d.end);
});
return body.data || {};
}
這裏是從服務中的數據:
[
{
"id": 1,
"name": "mein markt1",
"start": "2017-11-14T23:00:47.838+0000",
"end": "2017-12-14T23:00:47.838+0000",
"postalCode": "1100",
"city": "wien",
"address": "test",
"rating": 4.56,
"ratingPrice": 1.2,
"numberOfRatings": 10,
"latLng": {
"latitude": 48.208982,
"longitude": 16.373213
},
"openingHours": [
{
"open": true,
"start": 600,
"end": 1200,
"dayOfWeek": 0
},
{
"open": true,
"start": 660,
"end": 1200,
"dayOfWeek": 1
},
{
"open": true,
"start": 690,
"end": 1200,
"dayOfWeek": 2
},
{
"open": true,
"start": 600,
"end": 1200,
"dayOfWeek": 3
},
{
"open": true,
"start": 600,
"end": 1200,
"dayOfWeek": 4
},
{
"open": true,
"start": 600,
"end": 1200,
"dayOfWeek": 5
},
{
"open": true,
"start": 600,
"end": 1200,
"dayOfWeek": 6
}
],
"extraordinaryOpeningHours": [
]
}
]
是的謝謝,我不完全明白extractData現在也將我的類型作爲參數。謝謝! –
你實際錯過的是你根本不需要extractData方法--HttpClient爲你做。 – GreyBeardedGeek
@GreyBeadedGekk我嘗試了沒有extractData函數,沒有,它不起作用。它不會將它映射爲日期。當我打電話給start.getTime()時,我得到一個錯誤,那裏沒有這樣的函數 - 因爲它是一個String。 –