【问题标题】:How to Parse a Json Data Response from an API call in Ionic v3 and store in localStorage如何从 Ionic v3 中的 API 调用解析 Json 数据响应并存储在 localStorage
【发布时间】:2020-08-08 07:17:35
【问题描述】:

我想访问数组数据并存储在 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) 显示

{"data":[{
"loginStatus":"Valid",
"CustomerName":"Fagbemi Ayodele",
"Mobile":null,
"Email":"fagbemiayodele48@gmail.com",
"CustomerID":"3"
}]}

我需要解析 JSON 数据以获取单个 'value'

我尝试通过this.data.loginStatus 获取 loginStatus,但它给出了 null 值,对其他人也是如此。

请问,有人可以告诉我如何在 ionic 3 中获取数据个体值吗?

谢谢。

【问题讨论】:

    标签: json ionic-framework ionic3


    【解决方案1】:

    你可以这样得到的个人值:

    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
    

    【讨论】:

    • 谢谢先生,我能够通过将 data 转换为数组来解决它,即 data: Array;
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    相关资源
    最近更新 更多