简体   繁体   English

如何从JavaScript多维数组检索值

[英]How to retrieve value from javascript multidimensional array

I'm having trouble to retrieve the value from a javascript Object returned to me by a ajax call to a php webservice. 我在从通过php webservice的ajax调用返回给我的javascript对象中检索值时遇到麻烦。 I confirmed that the JSON returned is correct as you can see below: 我确认返回的JSON是正确的,如下所示:

{
    "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 . 我正在尝试使用response.json[i].value获取对象,但是每当我尝试提醒它时,我都会得到消息undefined And here is the funcition I'm using to the ajax call: 这是我在ajax调用中使用的功能:

webservice.js 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! 提前解冻!

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: json[i]是此对象:

{
    "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 ... 因此,您必须先访问campeonato属性,然后才能访问其属性,例如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); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM