How to access data from json response?


#1

Hello,
This is my otpProvider.ts

export class OtpproviderProvider {

phone:any;
institute:any;

constructor(public http: HttpClient) {
console.log(‘Hello UserProvider Provider’);
}
apiUrl=‘http://122.166.234.42:821/requestotp.php’;
getUsers(phonenumber,institute)
{
this.phone=phonenumber;
this.institute=institute;
console.log(this.phone);
return new Promise(resolve =>{
this.http.get(this.apiUrl+’/’+‘phone’+’=’+this.phone+‘institute’+’=’+this.institute).subscribe(data =>{
resolve(data);
},err => {
console.log(err);
});
});

}

}

and this is my institute.ts

constructor(public navCtrl: NavController, public navParams: NavParams,public userProvider:UserProvider,public alertCtrl: AlertController,public otpProvider:OtpproviderProvider) {
this.num=navParams.get(‘data’);

this.userProvider.getUsers(this.num).then(data =>{
this.instituteArray=data;
console.log(‘Institute Details are’+JSON.stringify(this.instituteArray));

});

}

I am sending Http request and getting response [{“status”:“success”}] in json…from this json how can i get the value of status???

please help…

Thanks


#2

json can start with array, but its wierd to have json response that start with array

data[0]["status"]

you will get “success”


#3

Hi, @ayusmapattanayak

Try this:

let isStatus = data['status'];
console.log('isStatus',isStatus);

#4

Thank you for your reply…
But it is giving status undefined


#5

Thank you for the reply…

it is giving status undefined


#6

in your UserProvider.getUsers()


getUsers(){
return this.http.get(this.apiUrl+’/’+‘phone’+’=’+this.phone+‘institute’+’=’+this.institute).toPromise()
}

pay attenttion to toPromise() rather then return new Promise then resolve.
to use .toPromise() you must import {Observable} from "rxjs/Observable";
later in institute.ts can

userProvider.getUsers().then(data=>{
console.log(data[0]['status'])
})


#7

It is working…Thank you.


#8

you’re welcome

you can also just return without toPromise()

it will return an observable instead

later in institute.ts can use subscribe instead of then
userProvider.getUsers().subscribe(data=>{})