无法从API访问特定的JSON数据

时间:2019-06-25 16:58:19

标签: json angular

我正在尝试从API-https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json获取数据。和https://www.fantasylabs.com/api/sportevents/3/2019_06_17

我已经使用forkJoin连接了2个API

从API- https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json中,我可以成功访问line列。但是,在检索数据时,它会提取名称为line的所有数据。我只想访问每个游戏的第一行。例如第一场比赛的"line": "+3.5000"。然后"line": "+46.0000"进行第二场比赛,依此类推

我还发现很难访问两支球队的赔率美国数据。例如,在第一游戏中,我感兴趣的数据是第一游戏的"oddsAmerican": "+148""oddsAmerican": "-182"

这是我的代码

y.component.ts

import {Component} from '@angular/core';
import {HttpClient} from '@angular/common/http'
import {forkJoin} from 'rxjs';
import {map} from 'rxjs/operators';

@Component({
  selector: 'app-mlb-api',
  templateUrl: './mlb-api.component.html',
  styleUrls: ['./mlb-api.component.css']
})
export class MlbApiComponent  {
allName;
//allawayTeamName;
allline;
allmoneyLine;
//all: Array<{line: string, awayTeam: string, homeTeam: string}> = [];
all: Array<{line: string, name: string, moneyLine:string}> = [];

  constructor(private http: HttpClient) {}

  ngOnInit() {

    const character = this.http.get('https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json').pipe(map((re: any) => re.events));
    const characterHomeworld = this.http.get('https://www.fantasylabs.com/api/sportevents/3/2019_06_17');

    forkJoin([character, characterHomeworld]).subscribe(([draftkingsResp, fantasylabsResp]) => {      

      this.allName = draftkingsResp.map(r => r.name);
      //this.allawayTeamName = draftkingsResp.map(r => r.awayTeamName);
      this.allline = draftkingsResp.map(r=>r.offers).flat().map(r => r.outcomes).flat().map(o => o.line);

      this.allline = this.allline.filter(l => !!l);

      this.allmoneyLine = draftkingsResp.map(r=>r.offers).flat().map(r => r.outcomes).flat().map(o => o.oddsAmerican);
      this.createAllArray();
    });
  }

  createAllArray(): void {
    for (let i = 0; i < this.allline.length; i++) {
      let item = {
        line: this.allline[i],
        name: this.allName[i],
        moneyLine: this.allmoneyLine[i]
        //homeTeam: this.allhomeTeamName[i]
      }
      this.all.push(item);
    }
  }
}

y.component.html

<table class="table table-striped table-condensed table-hover">
  <thead>
      <tr>

          <!-- <th class="awayTeamName">awayTeamName&nbsp;<a ng-click="sort_by('awayTeamName')"><i class="icon-sort"></i></a></th>
          <th class="field3">homeTeam&nbsp;<a ng-click="sort_by('HomeTeam')"><i class="icon-sort"></i></a></th> -->
          <th class="name">Name&nbsp;<a ng-click="sort_by('name')"><i class="icon-sort"></i></a></th>
          <th class="line">Line&nbsp;<a ng-click="sort_by('line')"><i class="icon-sort"></i></a></th>
          <th class="line">Money Line&nbsp;<a ng-click="sort_by('money_line')"><i class="icon-sort"></i></a></th>
      </tr>
  </thead>

  <tbody>
    <ng-container *ngFor="let item of all | paginate: { itemsPerPage: 5, currentPage: p }; let i = index">
      <tr>

        <td>{{item.name}}</td>

        <td>{{item.line }}</td>
         <td>{{item.moneyLine}}</td>

      </tr>
    </ng-container>
  </tbody>
</table> 



<pagination-controls (pageChange)="p = $event"></pagination-controls>

1 个答案:

答案 0 :(得分:0)

要获得line和oddsAmerican的首次出现,您可以这样操作:

ngOnInit() {

    const character = this.http.get('https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json').pipe(map((re: any) => re.events));
    const characterHomeworld = this.http.get('https://www.fantasylabs.com/api/sportevents/3/2019_06_17');

    this.firstLinePerGame = new Array<string>();
    this.oddsAmericans = new Array<string>();

    forkJoin([character, characterHomeworld]).subscribe(([draftkingsResp, fantasylabsResp]) => {      

      this.allhomeTeamName = draftkingsResp.map(r => r.homeTeamName);
      this.allawayTeamName = draftkingsResp.map(r => r.awayTeamName);
      this.allline = draftkingsResp.map(r=>r.offers).flat().map(r => r.outcomes).flat().map(o => o.line);
      this.allline = this.allline.filter(l => !!l);
      this.createAllArray();      

      draftkingsResp.forEach(r => {

        if(r.offers && r.offers.length) {

          if(r.offers.length === 3) {
            const firstGame = r.offers[0].outcomes[0];
            this.firstLinePerGame.push(firstGame.line);

            const secondGame = r.offers[2].outcomes[0];
            this.firstLinePerGame.push(secondGame.line);

            this.oddsAmericans.push(r.offers[1].outcomes[0].oddsAmerican)
            this.oddsAmericans.push(r.offers[1].outcomes[1].oddsAmerican)

          } else if(r.offers.length === 1) {

            const firstGame = r.offers[0].outcomes[0];
            this.firstLinePerGame.push(firstGame.line);

          } else if(r.offers.length === 2) {
            console.log('******************')
          }
        }
      })      

      console.log(this.firstLinePerGame.filter(l => l));
      console.log(this.oddsAmericans);
    });
  }

请参阅堆栈闪电战:https://stackblitz.com/edit/stackoverflow-24-06-2019-tz4ibr?file=src/app/app.component.ts