繁体   English   中英

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

[英]how to get a specific field in a complex json response

我在 parseserver - Back4app 中使用云代码,它使用 node.js 来请求 Football API。我通过 axios 请求获得了以下结果 (resultado):

{
    "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、地址等。使用 Javascript。

我尝试了很多方法,但没有一种对我有用。

按照我的代码:

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;

 // 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', } }; const resultado = axios.request(options).then(function(response) { const data = response.data.response data.forEach(function(team) { console.log(team) // "team" variable here contains the "team" and "venue" objects for each team }) }).catch(function(error) { return ("erro meu nobre;"); })
 <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM