1

I am new Ionic and I am not able to access a single value from json object. I want to console log the value of timestamp which I get from API.

My TS code is something like this

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();
                                 });
                                 }

My API looks like this:

{
"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"
}

How to access timestamp value in console log?

halfer
  • 18,701
  • 13
  • 79
  • 158
Saif khan
  • 65
  • 1
  • 9

1 Answers1

2

so here is the answer(map to json was missing)

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);

or

console.log(response['timestamp']);

should print the timpstamp.

Narendra
  • 4,264
  • 2
  • 17
  • 35
  • Can you let us know what is console.log(response); printing? – Narendra Oct 08 '18 at 06:09
  • _body: "{"status":"OK","message":"","countryCode":"IN","countryName":"India","zoneName":"Asia\/Kolkata","abbreviation":"IST","gmtOffset":19800,"dst":"0","zoneStart":-764145000,"zoneEnd":0,"nextAbbreviation":"","timestamp":1538998984,"formatted":"2018-10-08 11:43:04"}" __proto__: Body constructor: ƒ Response(responseOptions) toString: ƒ () __proto__: Object – Saif khan Oct 08 '18 at 06:14
  • this is the body of response which i printing. – Saif khan Oct 08 '18 at 06:14
  • i have defined data: any = {} ; in my ts file – Saif khan Oct 08 '18 at 06:21
  • couple of thing you can try here 1. map response e.g. .map(res => res.json()) or 2. try to directly access this.data = response._body – Narendra Oct 08 '18 at 06:23
  • visit this one for more information https://stackoverflow.com/questions/43394144/angular-2-how-to-access-an-http-response-body – Narendra Oct 08 '18 at 06:26
  • thank you sir now its working..map(res => res.json()) solve my problem. – Saif khan Oct 08 '18 at 06:28
  • Great, I have updated the answer so It might be helpful for other as well :) – Narendra Oct 08 '18 at 06:30