无法从API访问特定的JSON数据 [英] Cannot access specific JSON data from API

查看:61
本文介绍了无法从API访问特定的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetch data from API - https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json. and https://www.fantasylabs.com/api/sportevents/3/2019_06_17

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

I have jointed 2 API using forkJoin

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

From API- https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json I am successful in accessing line column. But while retrieving data it fetches all data where name is line. I just want to access 1st occurence of line for each game. For eg "line": "+3.5000" for 1st game. Then "line": "+46.0000" for second game and so on

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

I am also finding it difficult to access oddsAmerican data for both the teams. For eg in 1st game data I am interested in fetching are "oddsAmerican": "+148" and "oddsAmerican": "-182" for 1st game

这是我的代码

y.component.ts

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

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的首次出现,可以执行以下操作:

To get the first occurrence of line and oddsAmerican, you can do like this:

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

See the stackblitz: https://stackblitz.com/edit/stackoverflow-24-06-2019-tz4ibr?file=src/app/app.component.ts

这篇关于无法从API访问特定的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆