Pulling data in JSON

0

I have a question about JSON.

I need to pull the information inside the array. But I do not have the reference that would be the name of champion that is inside the date I need to find, the only thing you have is the key .

JSON Base:

{  
   "type":"champion",
   "format":"standAloneComplex",
   "version":"7.18.1",
   "data":{  
      "Aatrox":{  
         "id":"Aatrox",
         "key":"266",
         "name":"Aatrox",
         "title":"a Espada Darkin",
         "image":{  
            "full":"Aatrox.png",
            "sprite":"champion0.png",
            "group":"champion",
            "x":0,
            "y":0,
            "w":48,
            "h":48
         }
      },
      "Viktor":{  
         "id":"Viktor",
         "key":"112",
         "name":"Viktor",
         "title":"o Arauto das Máquinas",
         "image":{  
            "full":"Viktor.png",
            "sprite":"champion4.png",
            "group":"champion",
            "x":0,
            "y":0,
            "w":48,
            "h":48
         }
      }
   }
}

I'm using javascript language with jquery. Pulling the data does not locate. How do I resolve this?

$.ajax({
    type:'GET',
    url: 'http://ddragon.leagueoflegends.com/cdn/7.18.1/data/pt_BR/champion.json',
    dataType:'json',
    success: function(data) {
        var arr = [];
        arr.push(data);
        for(var i=0; i<arr.length; i++) {
            if(arr[i].data.key === 266) {
                console.log(arr[i].data.name);
            }
        }
    },
    error: function(data) { }
});
    
asked by anonymous 16.09.2017 / 10:20

2 answers

1

You have to iterate the keys of the data property. You can use Object.keys or for(var key in object) .

An example would be:

var data = {
  "type": "champion",
  "format": "standAloneComplex",
  "version": "7.18.1",
  "data": {
    "Aatrox": {
      "id": "Aatrox",
      "key": "266",
      "name": "Aatrox",
      "title": "a Espada Darkin",
      "image": {
        "full": "Aatrox.png",
        "sprite": "champion0.png",
        "group": "champion",
        "x": 0,
        "y": 0,
        "w": 48,
        "h": 48
      }
    },
    "Viktor": {
      "id": "Viktor",
      "key": "112",
      "name": "Viktor",
      "title": "o Arauto das Máquinas",
      "image": {
        "full": "Viktor.png",
        "sprite": "champion4.png",
        "group": "champion",
        "x": 0,
        "y": 0,
        "w": 48,
        "h": 48
      }
    }
  }
};
var arr = []; // não sei bem porque precisas disto? é só para o exemplo pois já tens outra "arr no código que vais acrescentar com "data"?
arr.push(data);
for (var i = 0; i < arr.length; i++) {
  var obj = arr[i].data;
  Object.keys(obj).forEach(function(prop) {
    if (obj[prop].key == 266) {
      console.log(obj[prop].name);
    }
  });
}

Notice that your JSON returns the value of key in String! So you can not use === if you compare with a number. Or use a string like this: if (obj[prop].key == '266') {

    
16.09.2017 / 10:25
0

I was able to make callback work. But I made a function called champion () where it will just add id , find the name and thus return the value.

But this return obj[prop].name; is bringing results like undefined . Testing console.log(obj[prop].name) is working perfectly. But why does the value return as undefined ?

function callback(andress, fn){        
    $.ajax({
        url: andress,
        type: 'GET',
        dataType: 'json',
        error: function(){},
        success: fn
    });
}

function champion(id) {
    callback('http://ddragon.leagueoflegends.com/cdn/7.18.1/data/pt_BR/champion.json', function(retorno) {
        var arr = [];
        arr.push(retorno);
        for (var i = 0; i < arr.length; i++) {
            var obj = arr[i].data;
            Object.keys(obj).forEach(function(prop) {
                if (obj[prop].key == id) {
                    return obj[prop].name;
                }
            });
        }
    });
}

callback(url+'/lol/summoner/v3/summoners/by-name/'+hash.name+'?api_key='+key, function(retorno) {
    callback(url+'/lol/match/v3/matchlists/by-account/'+retorno.accountId+'/recent?api_key='+key, function(invocador) {
        var itens = '';
        for (var i = 0; i < invocador.matches.length; i++) {
            itens += '<li>'+champion(invocador.matches[i].champion)+'</li>';
        }
        $('.partidas_recentes').html(itens);
    });

    $('.mdl-layout-title').text(retorno.name);
});
    
16.09.2017 / 13:40