首页 > 解决方案 > 在 IONIC 框架中使用 Angular JS 从 API 访问 JSON 数据

问题描述

大家好,我目前正在开发一个移动应用程序来获取实时的 covid-19 详细信息。为此,我使用了带有 angularjs 的 ionic 框架。我从数据正在获取的API中获取数据。

结果如下。我需要获取重要的详细信息,但我无法访问它。我需要获取 local_new_cases 和 local_total_Cases。

我应该如何得到它?

这是当前输出。我的代码显示在下面。home_page.ts`。这里我没有包含导入模块。

'

export class HomePage {
  data: string;
  error: string;
  loading: any;

  constructor(private http: HttpClient,public loadingController: LoadingController) {
    this.data = '';
    this.error = '';}
  
 async ionViewWillEnter() {'

    await this.presentLoading();

    // Load the data

    this.prepareDataRequest()

   .pipe(

      finalize(async () => {

          // Hide the loading spinner on success or error

          await this.loading.dismiss();
      })
  )
      .subscribe(
         data => {
          // Set the data to display in the template
          this.data = JSON.stringify(data);
                
        },
        err => {
          // Set the error information to display in the template
          this.error = `An error occurred, the data could not be retrieved: Status: ${err.status}, Message: ${err.statusText}`;
        }
      );
  }


   'async presentLoading()

 {
    // Prepare a loading controller

    this.loading = await this.loadingController.create({

        message: 'Loading...''
    



});

    // Present the loading controller
  await this.loading.present();
}

  private prepareDataRequest(): Observable<object> {
    // Define the data URL
    
    //const dataUrl = 'https://api.nigelbpeck.com/';
    const dataUrl = 'https://hpb.health.gov.lk/api/get-current-statistical/';
    // Prepare the request
    return this.http.get(dataUrl);
  }

}

`我需要获取的 JSON 数据。

标签: jsonangularjsapiionic-frameworkionic4

解决方案


推荐阅读