首页 > 解决方案 > Bittrex API 返回“结果”[object Object]

问题描述

使用 Angular 6 我无法弄清楚如何从 Bittrex API 请求https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-ltc显示“结果”数组

我可以获取 API,没问题并显示对象,但结果数组像 [object Object]

成功 true
消息
结果 [object Object]

这是HTML代码:

<tr *ngFor="let price of objectKeys(prices)">
  <td>{{ price }}</td>
  <td>{{ prices[price] }}</td>
</tr>

我只想知道正确的 HTML 代码来显示结果数组中的任何一个参数。任何帮助,将不胜感激。

谢谢

编辑:添加完整代码

数据服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';



@Injectable({
  providedIn: 'root'
})
export class DataService {


  result: any;

   constructor(private http: HttpClient) { }

   getPrice() {
    return this.http.get('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-ltc')

  }

}

组件.ts:

import { Component } from '@angular/core';
import { DataService } from './data.service';
import { Observable } from 'rxjs';

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

  objectKeys = Object.keys;
  prices: any;


  constructor(private data: DataService) {

  }

  ngOnInit() {
    this.data.getPrice()
      .subscribe(res => {
        this.prices = res;

      });


  }
}

组件.html

<h2>Bittrex Results</h2>
<div *ngIf="prices">
  <table id="pricetable">
    <tr>
      <th>Bittrex Close</th>
      <th>Bittrex Volume</th>
      <th>Test</th>

    </tr>
    <tr *ngFor="let price of objectKeys(prices)">
      <td>{{ price }}</td>
      <td>{{ prices[price] }}</td>
      <td>{{ prices[price] }}</td>
    </tr>
  </table>
 </div>

控制台日志

标签: apiangular6

解决方案


正如您在获得的 JSON 中看到的那样,该对象包含一个名为的属性,该属性result是包含价格的数组。

所以你必须使用这个属性,像这样:

this.data.getPrice()
  .subscribe(res => {
    this.prices = res.result;
  });

推荐阅读