How to traverse attributes of a JSON?

3

I'm trying to traverse attributes of a json, I'm trying that way

for (var i = 0, length = r.length; i < length; i++) {
   for (var i2 = 0,length = r[i].lenght; i2 < length; i2++ ){
       console.log("teste"+r[i][i2]);
   }
}    

How do I do this correctly?

Json is coming in irregularly, because when the service receives a null value from the bank it does not jump to the json field

[
{
    "nome": "Victor Siqueira",
    "login": "[email protected]",
    "senha": "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
    "tempoSessao": 15,
    "acesso": [
        {
            "idTela": "AA00"
        }
    ]
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "login": "as",
    "senha": "1234",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "login": "as2",
    "senha": "9876",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "login": "as3",
    "senha": "9876",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "login": "as4",
    "senha": "a123",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "login": "as5",
    "senha": "54d5cb2d332dbdb4850293caae4559ce88b65163f1ea5d4e4b3ac49d772ded14",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "login": "PFernandes",
    "senha": "54d5cb2d332dbdb4850293caae4559ce88b65163f1ea5d4e4b3ac49d772ded14",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
},
{
    "nome": "",
    "tempoSessao": 0,
    "acesso": []
}

]

    
asked by anonymous 27.10.2015 / 13:06

1 answer

3

I think the best way is for you to work with objects.

If you assign your JSON string to an object you can access it more easily.

var arrayJson = JSON.parse("tua string JSON");
arrayJson.forEach(function(pessoa){
  var acesso = pessoa.acesso;
  acesso.forEach(function(acesso)){
    if (acesso != null)
      //acesso.idTela - para acessar a propriedade pelo nome
  }
})

I do not know if I was clear, but any questions I can answer.

    
27.10.2015 / 14:34