JSON形式のHTTP要求の結果をTypeScriptのオブジェクトに保存する方法を教えてください。取得したJSONをオブジェクトとして保存する方法は?
は、ここに私のコードです:
コンポーネント:
import { Component } from '@angular/core';
import { DateTimeService } from './datetime.service';
import { DateTime } from './datetime'
@Component({
selector: 'my-app',
moduleId: module.id,
templateUrl: 'app.component.html',
providers: [DateTimeService]
})
export class AppComponent {
constructor(private _dateTimeService: DateTimeService){ }
dateTime: DateTime = new DateTime();
getDateTime() {
this._dateTimeService.getDateTime().subscribe(data => this.dateTime = data,
error => alert("error"),
() => alert("done"));
debugger;
alert(this.dateTime.date);
alert(this.dateTime.milliseconds_since_epoch);
alert(this.dateTime.time);
}
}
サービス:私はこのコードを実行するとDateTime
オブジェクトの
import { Http } from '@angular/http';
import {Injectable} from'@angular/core';
import 'rxjs/add/operator/map'
import { DateTime } from './datetime'
@Injectable()
export class DateTimeService{
constructor(private _http: Http) { }
getDateTime() {
return this._http.get('http://date.jsontest.com').map(res => <DateTime>res.json());
}
}
すべてのプロパティが定義されていません。しかし、私のサービスで入力データがDateTime
でキャストされず、JSON.stringify(data.time)
(たとえば)を使用した場合、1つのプロパティしか取得できません。
:
他のオプションは、あなたがそうのように使用することができ、前述の安全運航事業者、です。 – Dylan