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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (4)

我正在尝试从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": "+3.5000"对于第一场比赛。然后"line": "+46.0000"是第二场比赛等等

我也发现很难为两个团队访问oddsAmerican数据。比如,对于第一次的游戏数据我感兴趣的是取"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>
提问于
用户回答回答于

要获得第一次出现的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);
    });
  }

请参阅stackblitz:https://stackblitz.com/edit/stackoverflow-24-06-2019-tz4ibr file = src / app / app.component.ts

扫码关注云+社区

领取腾讯云代金券