Picking values with names in JSON

Asked

Viewed 16,723 times

4

I got this one JSON:

{"nome_rede":"lucasl","nome":"Lucas Lima","imagem":"http:\/\/intranet.supersoft.com.br\/novo\/usuarios\/fotos\/lucasl.jpg"}

How do I catch him using AJAX?

So far my code is like this, not catching anything.

            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/novo/engine/listarUsersGosteiPublicacaoFixa.php?id_publicacao="+$('#id_publicacao').val()+"", 
                success: function(data) {

                        for (var i=0;i<data.length;i++){
                            var imagem = data[i].imagem;
                            var nome = data[i].nome;
                            var nome_rede = data[i].nome_rede;

                        }

                }
            });

2 answers

5


You are trying to access in the wrong way, to access the data of this JSON you must do so:

$.ajax({
  type: "POST",
  dataType: "json",
  url: "/novo/engine/listarUsersGosteiPublicacaoFixa.php?id_publicacao=" + $('#id_publicacao').val() + "",
  success: function(data) {

    var imagem = data["imagem"];
    var nome = data["nome"];
    var nome_rede = data["nome_rede"];

  }
});

If JSON is returning an array of objects as in this example: [{"nome_rede":"lucasl","nome":"Lucas Lima","imagem":"img"}]'

the solution would be this:

$.ajax({
  type: "POST",
  dataType: "json",
  url: "/novo/engine/listarUsersGosteiPublicacaoFixa.php?id_publicacao=" + $('#id_publicacao').val() + "",
  success: function(data) {

    for (var i in data) { //vai passar por todos os objetos dentro do array
      imagem = data[i]["imagem"];
      nome = data[i]["nome"];
      nome_rede = data[i]["nome_rede"];
    }

  }
});

  • but if you have more than one piece of information? for example, two names, two images, two net_names, it has to be in the right loop?

  • One minute I’ll set it up for you

  • In this case ai data would not be an object, it would be an array

0

I tried to do as Antony Alkmim informed, but I could not, what worked for me was:

//Obtem o objeto httpRequest para fazer requisições AJAX, de acordo com o browser
function getHttpRequest(){
    if (window.XMLHttpRequest) {
        // Outros browsers
        req = new XMLHttpRequest();
    }else if (window.ActiveXObject) {
        // Internet Explorer
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return req;
}

// Busca endereço baseado no CEP
function preencheEndereco(event){
    event.preventDefault();
    var req = getHttpRequest();
    var cep = document.querySelector("input[name='CEPEndereco']");
    var url = "https://viacep.com.br/ws/" + cep.value + "/json/";
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == 200) {
                // Converte o retorno em JSON para um objeto válido
                var retorno = JSON.parse(req.responseText);
                LogradouroEndereco.value = retorno.logradouro;
                BairroEndereco.value =retorno.bairro;
                UFEndereco.value = retorno.uf;
                CidadeEndereco.value = retorno.localidade;
                IDCidadeEndereco.value = retorno.ibge;              
            }
        }
    }
    req.open("GET", url, true);
    req.send(null); 
}

Browser other questions tagged

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