Take Json data

1

I've been trying for a while to get the JSON of the API of League of Legends data and I can not find a way. I give the example of one of the JSON that I can not go.

{
    "23385178": {
        "summonerId": 23385178,
        "pages": [
            {
                "id": 34451272,
                "name": "Thresh",
                "current": false,
                "masteries": [
                    {
                        "id": 6342,
                        "rank": 1
                    },
                    {
                        "id": 6241,
                        "rank": 1
                    },
                    {
                        "id": 6311,
                        "rank": 5
                    },
                    {
                        "id": 6221,
                        "rank": 1
                    },
                    {
                        "id": 6211,
                        "rank": 5
                    },
                    {
                        "id": 6322,
                        "rank": 1
                    },
                    {
                        "id": 6332,
                        "rank": 5
                    },
                    {
                        "id": 6232,
                        "rank": 5
                    },
                    {
                        "id": 6362,
                        "rank": 1
                    },
                    {
                        "id": 6352,
                        "rank": 5
                    }
                ]
            },
            {
                "id": 34451273,
                "name": "Alistar",
                "current": false,
                "masteries": [
                    {
                        "id": 6342,
                        "rank": 1
                    },
                    {
                        "id": 6311,
                        "rank": 5
                    },
                    {
                        "id": 6241,
                        "rank": 1
                    },
                    {
                        "id": 6221,
                        "rank": 1
                    },
                    {
                        "id": 6211,
                        "rank": 5
                    },
                    {
                        "id": 6322,
                        "rank": 1
                    },
                    {
                        "id": 6332,
                        "rank": 5
                    },
                    {
                        "id": 6231,
                        "rank": 5
                    },
                    {
                        "id": 6352,
                        "rank": 5
                    },
                    {
                        "id": 6362,
                        "rank": 1
                    }
                ]
            }
        ]
    }
}

The JSON I'm treating as I read in another post here.

$(document).ready(function() {
    $.ajax({
        url: "https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/23385178/masteries?api_key=RGAPI-de016c14-78f2-4e19-82a7-33526d8bed4c",
        dataType: "json",
    }).then(function(data) {
        $('.parrId').append();
        $('.parrName').append();
        $('.parrIcon').append();
        $('.parrLvl').append();
    });
});

My intention is to place in the different paragraphs (parrId, parrName, etc) the information extracted from JSON . Obviously I would have to go through it to extract all the information, but I am unable to extract the first element, I think the name of array is bothering me.

    
asked by Marchena14 03.04.2017 в 19:47
source

1 answer

3

The object of the answer has a fixed key, which is: 23385178 , so to be able to go through the object and display the desired information, you should do the following:

$(document).ready(function() {
  $.ajax({
    url: "https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/23385178/masteries?api_key=RGAPI-de016c14-78f2-4e19-82a7-33526d8bed4c",
    dataType: "json",
  }).then(function(data) {
    // Definimos nuestro arreglo pages respecto a la clave fija
    var pages = data['23385178'].pages;
    // Recorremos nuestro arreglo pages
    for (var i = 0; i < pages.length; i++) {
      // Creamos un objeto para cada pocisión
      var obj = pages[i];
      // Ponemos nuestro resultado
      $('#result').append('<b>Id: </b>' + obj.id + ' <b>Name: </b>' + obj.name + '<br />');
      // Realizamos otro ciclo anidado recorriendo
      // el arreglo masteries dentro de nuestro objeto actual
      for (var j = 0; j < obj.masteries.length; j++) {
        // Ponemos nuestro resultado
        $('#result').append('<b>Id: </b>' + obj.masteries[j].id + ' <b>Rank: </b>' + obj.masteries[j].rank + '<br />');
      }
      $('#result').append('<hr />')
    }
  });
});
#result {
  font-family: monospace;
  padding: 5px 5px 5px 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>
    
answered by 03.04.2017 / 20:01
source