Pull data in JSON

Asked

Viewed 1,017 times

0

I have a question about JSON.

I need to pull the information inside the array. However it does not have the reference that would be the name of Champion that is within the date that I need to locate, the only thing it possesses is the key.

Base of JSON:

{  
   "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 am using javascript language with jquery. When pulling the data does not locate. How to 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) { }
});

2 answers

1


You have to iterate the keys to the property data.
You can use the Object.keys or the 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);
    }
  });
}

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

  • With your help you solved my problem! But when I create a function, the variable id does not recognize. Just to warn not know much in javascript, I am in the learning phase.

  • I’m here kicking my celebro to understand how to put the variable inside Function. I’m not getting the logic.

  • @Augustojunior what is the code that needs this variable? Is it a function? Put a jsFiddle with the code here to see.

0

I managed to make callback work. But I did a function called Champion() where you will just add id, locate the name and return the value.

But this condition return obj[prop].name; is bringing results as Undefined. When testing console.log(obj[prop].name) is working perfectly. But because the value returns 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);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.