Get JSON object dynamically

1

I have jSON that I created and wanted to dynamically grab the array and objects inside it

var json = [
    {'PHP': 
        [
            {'aula1':
                [
                'txt','O PHP é uma linguagem ...',
                'img','caminho','descrição'
                ]
            },
            {'aula2':
                [
                'txt','Nessa aula aprenderemos ...',
                'btn','Download','caminho','nome'
                ]
            }
        ]
    },
    {'HTML':
        [
            {'aula1':
                [
                'txt','HTML significa linguagem ...'
                ]
            },
            {'aula2':
                [
                'txt','Nessa aula aprenderemos ...',
                'btn','Download','caminho','nome'
                ]
            }
        ]
    }
]

something like:

var materia = 'PHP'
var numMateria = 0
var aula = 'aula1'
var numAula = 0

for(i = 0; i < json.[indexMateria].materia[indexAula].aula.length; i++) {
    if(json.[indexMateria].materia[indexAula].aula[i] == 'txt') {
        document.write('<p>' + json.[indexMateria].materia[indexAula].aula[i+1] + '</p>')
        i += 1
    } else if(json.[indexMateria].materia[indexAula].aula[i] == 'img') {
        document.write('<img src="' + json.[indexMateria].materia[indexAula].aula[i+1] + '" alt="' + json.[indexMateria].materia[indexAula].aula[i+2] + '">')
        i += 2
    }
}

But when I use json[indexMateria].materia it looks for a materia object instead of using the materia

Does anyone have any idea what to do? No problem changing json or using frameworks

    
asked by anonymous 25.10.2017 / 14:03

3 answers

1

As stated in the response from avsinacio, you can get the index of an object in javascrit by means of [] , it gets more or less like this

var json = [
    {'PHP': 
        [
            {'aula1':
                [
                'txt','O PHP é uma linguagem ...',
                'img','caminho','descrição'
                ]
            },
            {'aula2':
                [
                'txt','Nessa aula aprenderemos ...',
                'btn','Download','caminho','nome'
                ]
            }
        ]
    },
    {'HTML':
        [
            {'aula1':
                [
                'txt','HTML significa linguagem ...'
                ]
            },
            {'aula2':
                [
                'txt','Nessa aula aprenderemos ...',
                'btn','Download','caminho','nome'
                ]
            }
        ]
    }];
    
var indice1 = 0;
var materia = "PHP";
var indice2 = 0;
var aula = "aula1";

//Caso o objeto exista
if(typeof json[indice1][materia][indice2][aula] !== "undefined"){
      console.log(json[indice1][materia][indice2][aula]);
}
    
25.10.2017 / 14:22
1

I'm not sure I understand your question very well, but when doing this:

json[indexMateria].materia

You're exactly looking for a 'materia' object in your JSON.

If I understood your purpose well, you should do something like this:

json[indexMateria][materia]

If possible detail a little more what you want to do.

    
25.10.2017 / 14:14
-1

Convert your json variable to Object and do the necessary processing with JavaScript.

var obj = JSON.parse(json);
    
25.10.2017 / 14:08