axios 如何在复杂json响应中获取特定字段

bvk5enib  于 2022-11-23  发布在  iOS
关注(0)|答案(1)|浏览(150)

我在parseserver -Back 4app中使用了云代码,它使用node.js来请求Football API。

{
    "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"
                }
            }, (repeat for more 783 X)

我如何循环响应,并获得所有“团队”和“场馆”的名称,ID,地址等。
我试过很多方法,但没有一个对我有效。
遵循我代码:

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);
      //  return "olá função "
    }).catch(function (error) {
         return("erro meu nobre!");
});

return resultado;

相关问题