首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法从API访问特定的JSON数据

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

Stack Overflow用户
提问于 2019-06-26 00:58:19
回答 1查看 598关注 0票数 2

我正在尝试从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列。但在检索数据时,它会获取name为line的所有数据。我只想访问每一场比赛的第一次出现的线路。例如第一场比赛的"line": "+3.5000"。然后使用"line": "+46.0000"进行第二个游戏,依此类推

我还发现很难访问两个团队的oddsAmerican数据。例如在第一场比赛中,我有兴趣获取的数据是第一场比赛的"oddsAmerican": "+148""oddsAmerican": "-182"

以下是我的代码

y.component.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56758807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档