如何在 Ionic v3 中解析来自 API 调用的 Json 数据响应并存储在 localStorage 中
How to Parse a Json Data Response from an API call in Ionic v3 and store in localStorage
我想访问数组数据并存储在localStorage 中。
我打了一个 API 电话,得到如下响应,显示在控制台上
我的 Api 提供商:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
let apiUrl = 'http://localhost:83/buildit/api';
@Injectable()
export class AuthServiceProvider {
constructor(public http: Http) { }
login(credentials) {
//let headers = new Headers();
return new Promise((resolve, reject) => {
this.http.post(apiUrl + '/login.php', JSON.stringify(credentials))
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
}
login.ts
doLogin() {
if(this.loginData.LoginID =='' || this.loginData.Password ==''){
let toast = this.toastCtrl.create({
message: "Enter username and password",
duration: 3000,
position: 'bottom',
dismissOnPageChange: true
});
toast.onDidDismiss(() => {
});
toast.present();
}
else if(this.loginData.LoginID!=='' && this.loginData.Password!==''){
this.showLoader();
this.authService.login(this.loginData).then((result) => {
this.data = result.data;
console.log(this.data.loginStatus);
if (this.data.loginStatus == 'Valid') {
localStorage.setItem('loginStatus', this.data.loginStatus);
localStorage.setItem('CustomerName', this.data.CustomerName);
localStorage.setItem('Mobile', this.data.Mobile);
localStorage.setItem('Email', this.data.Email);
localStorage.setItem('CustomerID', this.data.CustomerID);
this.navCtrl.setRoot(MyApp);
this.loading.dismiss();
}
else if (result['msg'] == "Please verify your mobile no. to Login") {
this.navCtrl.push(OtpPage,{
ID : result['data'].CustomerID , Mobile : this.loginData.LoginID,
Email: result['data'].Email
});
this.loading.dismiss();
}
else {
document.getElementById('err-span').style.display = "block";
this.loading.dismiss();
}
}, (err) => {
this.loading.dismiss();
this.presentToast(err);
});
}
}
Console.log(数据)显示
{"data":[{
"loginStatus":"Valid",
"CustomerName":"Fagbemi Ayodele",
"Mobile":null,
"Email":"fagbemiayodele48@gmail.com",
"CustomerID":"3"
}]}
我需要解析 JSON 数据以获取单个 'value'
我尝试 this.data.loginStatus
获取 loginStatus 但它给出空值,其他人也是如此。
拜托,有人可以告诉我如何在 ionic 3 中获取数据个体值吗?
谢谢。
您可以获得这样的个人价值:
data[0]['loginStatus']
localStorage.setItem('loginStatus', data[0]['loginStatus']);
localStorage.getItem('loginStatus');
//Valid
data[0]['CustomerName']
localStorage.setItem('CustomerName', data[0]['CustomerName']);
localStorage.getItem('CustomerName');
//Fagbemi Ayodele
data[0]['Mobile']
localStorage.setItem('Mobile', data[0]['Mobile']);
localStorage.getItem('Mobile');
//null
data[0]['Email']
localStorage.setItem('Email', data[0]['Email']);
localStorage.getItem('Email');
//fagbemiayodele48@gmail.com
data[0]['CustomerID']
localStorage.setItem('CustomerID', data[0]['CustomerID']);
localStorage.getItem('CustomerID');
//3
我想访问数组数据并存储在localStorage 中。
我打了一个 API 电话,得到如下响应,显示在控制台上
我的 Api 提供商:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
let apiUrl = 'http://localhost:83/buildit/api';
@Injectable()
export class AuthServiceProvider {
constructor(public http: Http) { }
login(credentials) {
//let headers = new Headers();
return new Promise((resolve, reject) => {
this.http.post(apiUrl + '/login.php', JSON.stringify(credentials))
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
}
login.ts
doLogin() {
if(this.loginData.LoginID =='' || this.loginData.Password ==''){
let toast = this.toastCtrl.create({
message: "Enter username and password",
duration: 3000,
position: 'bottom',
dismissOnPageChange: true
});
toast.onDidDismiss(() => {
});
toast.present();
}
else if(this.loginData.LoginID!=='' && this.loginData.Password!==''){
this.showLoader();
this.authService.login(this.loginData).then((result) => {
this.data = result.data;
console.log(this.data.loginStatus);
if (this.data.loginStatus == 'Valid') {
localStorage.setItem('loginStatus', this.data.loginStatus);
localStorage.setItem('CustomerName', this.data.CustomerName);
localStorage.setItem('Mobile', this.data.Mobile);
localStorage.setItem('Email', this.data.Email);
localStorage.setItem('CustomerID', this.data.CustomerID);
this.navCtrl.setRoot(MyApp);
this.loading.dismiss();
}
else if (result['msg'] == "Please verify your mobile no. to Login") {
this.navCtrl.push(OtpPage,{
ID : result['data'].CustomerID , Mobile : this.loginData.LoginID,
Email: result['data'].Email
});
this.loading.dismiss();
}
else {
document.getElementById('err-span').style.display = "block";
this.loading.dismiss();
}
}, (err) => {
this.loading.dismiss();
this.presentToast(err);
});
}
}
Console.log(数据)显示
{"data":[{
"loginStatus":"Valid",
"CustomerName":"Fagbemi Ayodele",
"Mobile":null,
"Email":"fagbemiayodele48@gmail.com",
"CustomerID":"3"
}]}
我需要解析 JSON 数据以获取单个 'value'
我尝试 this.data.loginStatus
获取 loginStatus 但它给出空值,其他人也是如此。
拜托,有人可以告诉我如何在 ionic 3 中获取数据个体值吗?
谢谢。
您可以获得这样的个人价值:
data[0]['loginStatus']
localStorage.setItem('loginStatus', data[0]['loginStatus']);
localStorage.getItem('loginStatus');
//Valid
data[0]['CustomerName']
localStorage.setItem('CustomerName', data[0]['CustomerName']);
localStorage.getItem('CustomerName');
//Fagbemi Ayodele
data[0]['Mobile']
localStorage.setItem('Mobile', data[0]['Mobile']);
localStorage.getItem('Mobile');
//null
data[0]['Email']
localStorage.setItem('Email', data[0]['Email']);
localStorage.getItem('Email');
//fagbemiayodele48@gmail.com
data[0]['CustomerID']
localStorage.setItem('CustomerID', data[0]['CustomerID']);
localStorage.getItem('CustomerID');
//3