【问题标题】:How do I do two api calls, receive the json then store this data in a local json file with a slightly different structure? (JavaScript)如何进行两次 api 调用,接收 json 然后将此数据存储在结构略有不同的本地 json 文件中? (JavaScript)
【发布时间】:2019-11-14 09:08:25
【问题描述】:

首先,我正在编写一个显示体育信息的小型 JS 应用程序。基本上,我正在使用一个包含所有球队和所有球员的 NBA api。我想做的是定期调用 AllTeams 和 AllPlayers 端点,接收数据,然后将数据存储在本地 JSON 文件中。

目前来自 AllTeams 端点的数据如下所示:

玩家数据如下:

但是在我的本地 json 文件中,我希望每个团队都有一个“玩家”键,然后根据 team.id 存储来自 AllPlayers 端点的玩家

只是为了澄清我在执行 api 调用等方面没有问题,我不确定如何为我的 json 文件设置一个结构并将来自两个端点的实际数据保存在该 json 中文件。

我想要的结构是这样的:

谢谢, 贝兹

【问题讨论】:

  • 您能提供一下您当前代码的外观吗?
  • 其实我还没有开始编码,因为我不知道正确的方法。
  • 为什么是本地?如果这是一个网页,并且您只想存储少于 10MB 的空间,那么您可以像 localStorage.players = JSON.stringify(obj); 那样做。喜欢var obj = JSON.parse(localStorage.players);。
  • allPlayers 是否包含每支球队的所有球员或比赛中的所有球员的列表?
  • 使用Node JS中的fs模块保存到文本文件中

标签: javascript json


【解决方案1】:

假设您已经从 api 获得了这些数据(并且已经对其进行了解析,因此您只有数组数据)

// data from api
const allTeams = [
    {
      id: 1,
      abbreviation: 'ATL',
      ... // other fields
    },
    {
      id: 2,
      abbreviation: 'BOS',
      ... // other fields
    },
]

// data from api
const players = [
  {
     id: 1,
     name: 'Buggy Sama',
     team: {
       id: 1,
       abbreviation: 'ATL'
       // other team fields
     }
     // other player fields
  }
]
  • 要形成您想要的数据,您可以通过地图将每个球员插入球队中:
const jsonPerTeam = allTeams.map(team => {
  const teamPlayers= {}

  // you can use any other loop functions here if you prefer that, 
  // but map is the fastest and just a personal preference
  players.map(player => {

    // if player's team is the same as the team, then store that player on player's key under teamPlayers
    if (player.team.id === team.id) {
      teamPlayers[player.id] = player;
    }
  })

   // add teamPlayers as players on your team
   return {
      ...team,
      players: teamPlayers
   }
});

^^ 上面的代码会生成你想要的结构(根据帖子)。

  • 要存储数据,您可以查看此answer

所以你可以这样做:

import fs from 'fs'

fs.writeFile("myLocalJsonTeam.json", jsonPerTeam , function(err) {
    if (err) {
        console.error(err);
    }
});

如果我理解你的问题,请澄清

【讨论】:

    【解决方案2】:

    首先,allPlayers 似乎被异化了,一次只返回 25 个玩家,我不知道这个数量是否可以改变,但如果不能确保你遍历每个页面并推送所有玩家到一个局部变量。

    var fs = require('fs');
    
    let allPlayers = []; // Contains list of all players
    const allTeams = []; // Contains list of all teams
    
    const getAllTeamsWithPlayers = allTeams.map( team => {
      // Get a list of all players that play for the team
      const allPlayersForTeam = allPlayers.filter((player, index) => {
        // Check if this player plays for the team
        const playsForTeam = player.team.id == team.id;
        // If so, delete this player from the list (that way in next iteration it doesn't have to be looped over again)
        if (playsForTeam) allPlayers.splice(index, 1);
        return playsForTeam;
      });
     team.players = allPlayersForTeam;
     return team;
    });
    
    // Convert object to json
    const allTeamsWithPlayersJSON = JSON.stringify(getAllTeamsWithPlayers());
    
    fs.writeFile('myjsonfile.json', allTeamsWithPlayersJSON, 'utf8', console.log);
    

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 2019-11-15
      • 2021-02-11
      • 2021-12-09
      相关资源
      最近更新 更多