1

I can get data from simple json, but I trying get data from a more complex (for me) json, and I can get initial data, but I have trouble with getting the rest of the data. My json (link):

0   
table   "C"
no  "043/C/NBP/2018"
tradingDate "2018-02-28"
effectiveDate   "2018-03-01"
rates   
0   
currency    "dolar amerykański"
code    "USD"
bid 3.3875
ask 3.4559
1   
currency    "dolar australijski"
code    "AUD"
bid 2.6408
ask 2.6942
2   
currency    "dolar kanadyjski"
code    "CAD"
bid 2.6468
ask 2.7002

I can get data like this table, no, tradingDate or effectiveDate, but I can`t get data from rates, ex. currency, code, bid or ask. How can I get this data from my json? My service:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import "rxjs/Rx";
import {Npb} from './npb';

import { Http , Response, HttpModule} from '@angular/http';

@Injectable()
export class NbpService {

  private _postsURL = "http://api.nbp.pl/api/exchangerates/tables/c/?format=json";

  constructor(private http: Http) {
  }

  getPosts(): Observable<Npb[]> {
      return this.http
          .get(this._postsURL)
          .map((response: Response) => {
              return <Npb[]>response.json();              
          })         
  }
  private handleError(error: Response) {
      return Observable.throw(error.statusText);
  }
}

Component:

import { Component, OnInit } from '@angular/core';
import {NbpService} from './nbp.service';
import {Npb} from './npb';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
  providers: [NbpService]
})
export class HomeComponent implements OnInit {
  _postsArray: Npb[];
  constructor(private NbpService: NbpService, ) {
  }
  getPost(): void {
    this.NbpService.getPosts()
        .subscribe(
            resultArray => this._postsArray= resultArray,
            error => console.log("Error :: " + error)
        )
}

ngOnInit(): void {
    this.getPost();
}
}

And html:

 <tr *ngFor="let post of _postsArray">

    <td>USD</td>
    <td>{{post.effectiveDate}}</td>
    <td>{{post.tradingDate}}</td>
    <td>{{post.ask}}</td>
  </tr>

And Nbp:

export interface Npb {
    date: string;
    buy: string;
    sel: string;
}
1
  • Could you please post class file of Npb Commented Mar 2, 2018 at 7:36

2 Answers 2

1

You can't get the value from ask like you did. Because ask is inside the rates array. You could do <td>{{post.rates[0].currency}}</td> that would give you the currency of the first element of the array.

If you want to print all rates you have to do another loop:

<tr *ngFor='let rate of post.rates'>
  <td>{{rate.currency}}</td>
  <td>{{rate.code}}</td>
  <td>{{rate.bid}}</td>
  <td>{{rate.ask}}</td>
</tr>
Sign up to request clarification or add additional context in comments.

Comments

1

when you subscribe you received

[table:..,rates:[{currency:aa,code:bb,}{{currency:aa,code:bb,}..]

that's:an array of one element, so

result[0]={table:..rates:[{...},{...}]}

and

result[0].rates={{...},{...}]

So

getPosts(): Observable<Npb[]> {
      //See that I use httpClient
      return this.httpClient
          .get(this._postsURL)
          .map((response: any) => {  //see that we received response:any
              return response[0].rates;              
          })         
  }

NOTE:Use httpClient NOT the "old" http, see https://angular.io/guide/http#httpclient

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.