如何在Ionic 3中控制台记录来自JSON对象的单个值

时间:2018-10-08 05:44:49

标签: angularjs ionic-framework

我是新离子用户,无法从json对象访问单个值。我想控制台记录我从api获取的时间戳的值。

我的ts代码是这样的

viewTime(){

  let url = 'http://api.timezonedb.com/v2.1/get-time-zone?key=8304HG08VTGQ&format=json&by=zone&zone=Asia/Kolkata';

        this.http.request(url, this.config.options)
            .subscribe((response: any) => {
                   console.log(response);

                   this.data = response ;

                  console.log(this.data.timestamp); // i want this value to console log


                let date = new Date()
                 var myDate = new Date(new Date().getTime()+(7*24*60*60*1000));
                 console.log(myDate);

                 console.log("Current Date ",date);

                  this.nav.setRoot(AccountLogin);

                                }, (err) => {
                                let alert = this.alertCtrl.create({
                                title: 'Error',
                                subTitle: 'Please check your zone',
                                buttons: ['OK']
                                        });
                                 alert.present();
                                 });
                                 }

我的api看起来像这样

{
"status": "OK",
"message": "",
"countryCode": "IN",
"countryName": "India",
"zoneName": "Asia/Kolkata",
"abbreviation": "IST",
"gmtOffset": 19800,
"dst": "0",
"zoneStart": -764145000,
"zoneEnd": 0,
"nextAbbreviation": "",
"timestamp": 1538838621,
"formatted": "2018-10-06 15:10:21"

}

如何在控制台日志中访问时间戳值。请帮帮我。

1 个答案:

答案 0 :(得分:1)

所以这就是答案(缺少与json的映射)

IAM

let url = 'http://api.timezonedb.com/v2.1/get-time-zone?key=8304HG08VTGQ&format=json&by=zone&zone=Asia/Kolkata';

        this.http.request(url, this.config.options)
.map(res => res.json()) 
            .subscribe((response: any) => {

}

console.log(response.timestamp);

应打印定标印章。