我的rest api中有以下json:
{"data":[{
"loginStatus":"Valid",
"CustomerName":"Fagbemi Ayodele","Mobile":null,
"Email":"fagbemiayodele48@gmail.com",
"CustomerID":"3"
}]}
在我的login.ts中,我试图通过以下操作将json中的每个值保存到localStorage中:
this.authService.login(this.loginData).then((result) => {
this.data = result;
localStorage.setItem('loginStatus', this.data.loginStatus);
localStorage.setItem('CustomerName', this.data.CustomerName);
this.navCtrl.setRoot(MyApp);
this.loading.dismiss();
}
当我尝试打印控制台中的值,即console.log(this.data.loginStatus);
时,它返回undefined
。
如何获取每个值?
这是我在存储中使用的方法,我希望它能对you 2有所帮助。
设定值
public set(keyValue,value){
return this.storage.set(keyValue,value);
}
获取值
public async get(keyValue){
return await this.storage.get(keyValue);
}
返回的JSON的data属性不是对象,而是数组。所以你的代码应该像这个
this.authService.login(this.loginData).then((result) => {
this.data = result[0];
localStorage.setItem('loginStatus', this.data.loginStatus);
localStorage.setItem('CustomerName', this.data.CustomerName);
this.navCtrl.setRoot(MyApp);
this.loading.dismiss();
}
请注意我是如何获得结果数组的0索引的。