Take JSON object dynamically

Asked

Viewed 447 times

1

I have json that I created and wanted to dynamically take 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
    }
}

Only when I wear json[indexMateria].materia he searches for an object materia instead of using the variable materia

Does anyone have any idea what to do? It’s okay to change json or use frameworks

  • Exactly what you want to seek?

  • the array within the class objects

3 answers

1

I’m not sure I understand your question very well:

json[indexMateria].materia

You are exactly searching for a 'materia' object in your JSON.

If I understood your goal correctly, you should do something like:

json[indexMateria][materia]

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

  • using [materia] it shows this error: Uncaught Syntaxerror: Unexpected token [

  • summarizing... I have a json with an array of matter objects (HTML and PHP for example), the subjects have an array of classes objects (Aula1, aula2, ...) and the classes have an array of contents. want to take this content and write in html

  • @Guilhermecostamilam this error of Unexpected token was caused by the mixing of Bracket Notation with dot Notation. In the passages where it has json.[indexMateria] use only json[indexMateria]

  • OK, thanks for the help man

1


As stated in the avsinacio response, you can pick up the index of an object in javascrit by means of the [], looks something 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]);
}

  • that’s right worth

-1

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

var obj = JSON.parse(json);
  • The "json" to which the questioner refers is already an object.

  • No, it is an Array, so when it tries to access the values with ( . ) error.

  • Fair. But still not the case with JSON.parse.

Browser other questions tagged

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