You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Back4app Parse Server云代码中遍历足球API JSON提取球队场馆信息

提取Football API返回的巴西球队与场馆信息

我在Back4app的Parse Server中使用Node.js编写云代码,通过Axios请求Football API,已获取到包含巴西球队的复杂JSON响应,但尝试多种方法均无法遍历并提取所有team和venue的名称、ID、地址等信息。

返回的JSON结构

{
    "result": {
        "get": "teams",
        "parameters": {
            "country": "brazil"
        },
        "errors": [],
        "results": 785,
        "paging": {
            "current": 1,
            "total": 1
        },
        "response": [
            {
                "team": {
                    "id": 6,
                    "name": "Brazil",
                    "code": "BRA",
                    "country": "Brazil",
                    "founded": 1914,
                    "national": true,
                    "logo": "https://media.api-sports.io/football/teams/6.png"
                },
                "venue": {
                    "id": 204,
                    "name": "Estadio Jornalista Mário Filho (Maracanã)",
                    "address": "Rua Professor Eurico Rabelo, Maracanã",
                    "city": "Rio de Janeiro, Rio de Janeiro",
                    "capacity": 78838,
                    "surface": "grass",
                    "image": "https://media.api-sports.io/football/venues/204.png"
                }
            },
            {
                "team": {
                    "id": 118,
                    "name": "Bahia",
                    "code": "BAH",
                    "country": "Brazil",
                    "founded": 1931,
                    "national": false,
                    "logo": "https://media.api-sports.io/football/teams/118.png"
                },
                "venue": {
                    "id": 216,
                    "name": "Arena Fonte Nova",
                    "address": "Rua Lions Club, Nazaré",
                    "city": "Salvador, Bahia",
                    "capacity": 56500,
                    "surface": "grass",
                    "image": "https://media.api-sports.io/football/venues/216.png"
                }
            }
        ]
}

当前云代码

Parse.Cloud.define("teams", (request)=>{
  const axios = require("axios");
  const time = request.params.time;
  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/teams',
    params: {country:'brazil'},
    headers: {
      'X-RapidAPI-Key': 'd69302225emshdf770c890926efdp19ca04jsn08d2244e2253',
      'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
     }
  };
      var resultado = axios.request(options).then( function (response) {
        return (response.data);
    }).catch(function (error) {
         return("erro meu nobre!");
});
return resultado;
})

解决方案

要实现信息提取,需在Axios的回调中定位到数据数组并遍历,具体修改如下:

  1. 定位到API响应中的球队数组:数据路径为response.data.result.response,这是包含所有球队和场馆信息的数组。
  2. 使用map方法遍历数组,提取每个元素中team和venue的目标字段。
  3. 优化错误处理,返回更详细的错误信息便于排查。

修改后的完整代码:

Parse.Cloud.define("teams", (request) => {
  const axios = require("axios");
  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/teams',
    params: { country: 'brazil' },
    headers: {
      'X-RapidAPI-Key': 'd69302225emshdf770c890926efdp19ca04jsn08d2244e2253',
      'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
    }
  };

  return axios.request(options).then(function (response) {
    // 定位到球队数据数组
    const teamsData = response.data.result.response;
    // 遍历提取所需信息
    const extractedInfo = teamsData.map(item => {
      return {
        team: {
          id: item.team.id,
          name: item.team.name,
          country: item.team.country,
          logo: item.team.logo
        },
        venue: {
          id: item.venue.id,
          name: item.venue.name,
          address: item.venue.address,
          city: item.venue.city,
          image: item.venue.image
        }
      };
    });
    return extractedInfo;
  }).catch(function (error) {
    // 返回详细错误信息
    return { error: error.message, details: error.response?.data || '无额外错误信息' };
  });
});

说明

  • map方法会遍历数组中的每个元素,将其转换为只包含所需字段的对象,最终返回一个新的提取后数据数组。
  • 可根据需求调整返回的字段,比如添加team.code或venue.capacity等。
  • 错误处理中加入了error.response?.data,可以获取API返回的具体错误信息,便于调试。

内容的提问来源于stack exchange,提问作者Duda Clash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:55:20