简体   繁体   English

如何从 JSON 数组中获取数据 - Angular 7+

[英]How to get data from array in JSON - Angular 7+

I'm completely beginning in Angular and API.我完全是从 Angular 和 API 开始的。 I'm trying to resolve some problem.我正在努力解决一些问题。 My task is simple i guess.我想我的任务很简单。 I have to get currency exchange rates data from NBP Web API.我必须从 NBP Web API 获取货币汇率数据。 JSON file looks like: JSON 文件如下所示:

在此处输入图片说明

I've found solution how to get some data from JSON, but i still have problem with array.我找到了如何从 JSON 获取一些数据的解决方案,但我仍然遇到数组问题。 The json file was converted to typescript and the result was : json 文件被转换为打字稿,结果是:

在此处输入图片说明

So my Code look's like that:所以我的代码看起来像这样:

Exchange.model.ts交换模型.ts

 export interface Exchange {
  table: string;
  no: string;
  effectiveDate: string;
  rates: Rate[];
}

export interface Rate {
    currency: string;
    code: string;
    mid: number;
  }

app.component.ts app.component.ts

import { Component, OnInit } from '@angular/core';
import { Exchange, Rate } from './exchange.model';

import { DataService } from './data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  exchanges$: Exchange;
  rates$: Rate;

  constructor(private dataService: DataService) {}

  ngOnInit() {
    return this.dataService.getExchanges().subscribe(data => this.exchanges$ = data)


  }

  ngOnInit2() {
    return this.dataService.getRates().subscribe(data => this.rates$ = data)


  }
}

and app.component.html和 app.component.html

<div *ngFor='let exchanges of exchanges$'>
        <h2">{{exchanges.table}}  <br> {{exchanges.no}} <br> {{exchanges.effectiveDate}}</h2">
            {{exchanges.rates}}

</div>

The browser display the data like that:浏览器显示这样的数据:

在此处输入图片说明

The result is almost good.结果几乎是好的。 But the data from "rates" in JSON doesn't work.但是来自 JSON 中“rates”的数据不起作用。

"rates":[ 
{ 
"currency":"bat (Tajlandia)",
"code":"THB",
"mid":0.1261
},"

How to solve a problem?如何解决问题? How to get data from array in JSON into typescript?如何从 JSON 数组中获取数据到打字稿中? What input into array in Exchange.module.ts?什么输入到 Exchange.module.ts 中的数组? I know that, a question is long, but I wanted to display necessary code and images.我知道,一个问题很长,但我想显示必要的代码和图像。

You already have an Array as you see it displays a list of objects on your page.您已经拥有一个Array ,它会在您的页面上显示一个对象列表。 Since Rate is also an object you need to display its properties to see something meaningful.由于Rate也是一个对象,因此您需要显示其属性才能看到有意义的内容。

If you want to display all the rates you can do another *ngFor for the rates like you do for exchanges.如果您想显示所有汇率,您可以像交换汇率一样为汇率做另一个*ngFor

For Example:例如:

<div *ngFor="let exchanges of exchanges$">
   <h2>{{exchanges.table}}  <br> {{exchanges.no}} <br> {{exchanges.effectiveDate}}</h2>
   <div *ngFor="let rate of exchanges.rates">{{rate.code}} {{rate.mid}} {{rate.currency}}</div>
</div>

You did not explain very well what kind of output you want to get.你没有很好地解释你想要得到什么样的输出。 If you want some additional processing it is better you move it inside the component code.如果您想要一些额外的处理,最好将其移动到组件代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM