0

I'm having trouble to retrieve the value from a javascript Object returned to me by a ajax call to a php webservice. I confirmed that the JSON returned is correct as you can see below:

{
    "json": [{
        "campeonato": {
            "id_campeonato": "630",
            "nome_campeonato": "Copa do Mundo\r",
            "nome_pais": "África\r"
        }
    }, {
        "campeonato": {
            "id_campeonato": "11",
            "nome_campeonato": "Série A\r",
            "nome_pais": "Brasil\r"
        }
    }]
}

I'm trying to get the object using response.json[i].value but whenever I try to alert this I get the message undefined. And here is the funcition I'm using to the ajax call:

webservice.js

function listaCampeonatos(){
    $.ajax({
        url: 'http://localhost/projetos/centraljogos/webservice/listagem.php',
        type: 'GET',
        dataType: 'json',
        data: {type:'listaCampeonatos'},
        ContentType: 'application/json',
        success: function(response){
            //alert('Listagem bem sucedida!');
            //$('#resultado').html(JSON.stringify(response));

            //console.log(response);

            alert(JSON.stringify(response));

            for (i=0 ; i<response.json.length ; i++){
                //alert('Entrou no for / Pos. array: '+i);
                console.log(response.json[i].nome_campeonato);
                alert(response.json[i].nome_campeonato);
            }
        },
        error: function(err){
            alert('Ocorreu um erro ao se comunicar com o servidor! Por favor, entre em contato com o administrador ou tente novamente mais tarde.');
            console.log(err);
        }
    });
}

So, what am I doing wrong? Thaks in advance!

1
  • I see no value property in your json object. Try this: response.json[i].campeonato.id_campeonato Commented Oct 7, 2016 at 18:40

1 Answer 1

1

Try this:

for (i=0 ; i<response.json.length ; i++){
    //alert('Entrou no for / Pos. array: '+i);
    console.log(response.json[i].campeonato.nome_campeonato);
    alert(response.json[i].campeonato.nome_campeonato);
}

json[i] is this object:

{
    "campeonato": {
        "id_campeonato": "630",
        "nome_campeonato": "Copa do Mundo\r",
        "nome_pais": "África\r"
    }
}

So you have to access campeonato property before access it's properties like id_capeonato...

Working snippet:

var response = {
    "json": [{
        "campeonato": {
            "id_campeonato": "630",
            "nome_campeonato": "Copa do Mundo\r",
            "nome_pais": "África\r"
        }
    }, {
        "campeonato": {
            "id_campeonato": "11",
            "nome_campeonato": "Série A\r",
            "nome_pais": "Brasil\r"
        }
    }]
};

// Directly
console.log(response.json[0].campeonato);

// Within a loop
for (var i = 0; i < response.json.length; i++) {
  console.log(response.json[i].campeonato);
}

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I thought that the 'json[i]' would already enter the 'campeonato' object. How dummy am I? Thanks man!
@GuilhermeRamalho it's ok, np. De br pra br (y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.