How to repeat Json data in a list

Asked

Viewed 103 times

0

How to list all students(name), series(degreeId) and classes(classId) in this Json file.

[
{
    "id":1,
    "ra":12346,
    "name":"Pedro Santos Neves",
    "degreeId":1,
    "classId":1
},
{
    "id":2,
    "ra":456798,
    "name":"Maria Antônia Silva",
    "degreeId":2,
    "classId":1
},
{
    "id":3,
    "ra":752156,
    "name":"José Carlos do Prado",
    "degreeId":3,
    "classId":2
},
{
    "id":4,
    "ra":852348,
    "name":"Aparecido Costa Ribeiro",
    "degreeId":4,
    "classId":2
},
{
    "id":5,
    "ra":454643,
    "name":"Mariana Antunes Aguiar",
    "degreeId":6,
    "classId":2
},
{
    "id":6,
    "ra":192837,
    "name":"Ester Faria de Cassio",
    "degreeId":5,
    "classId":2
} ]

Follow the Javascript

function loadDoc() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                var students = JSON.parse(this.responseText);
                var degrees = JSON.parse(this.responseText);
                var classes = JSON.parse(this.responseText);
                    document.getElementById("aluno").value = students[0].name;
                    document.getElementById("serie").value = degrees[0].degreeId;
                    document.getElementById("classe").value = classes[0].classId;
            }
        };
        xmlhttp.open("GET", "students.json", true);
        xmlhttp.send();
    }

Follows the HTML

<div class="container">
    <form class="col l12 s12">
        <div class="row">
            <div class="input-field col l6 s7">
                <input placeholder="Aluno" id="aluno" type="text" class="validate">
                <label for="aluno"></label>
            </div>
            <div class="input-field col l3 s2">
                <input placeholder="Série" id="serie" type="text" class="center validate">
                <label for="serie"></label>
            </div>  
            <div class="input-field col l3 s2">
                <input placeholder="Classe" id="classe" type="text" class="center validate">
                <label for="classe"></label>
            </div>       
        </div>
    </form>
</div>

<div class="container">
    <div class="col s12">
        <div class="row">
            <a class="waves-effect waves-light btn left" onclick="loadDoc()">Carregar</a>
            <a class="waves-effect waves-light btn right">Editar</a>
        </div>
    </div>
</div>

Screenshot of the screen to load inputs

inserir a descrição da imagem aqui

  • You can give an example of how you want the resulting HTML to be?

  • Sergio edited my question and put the HTML. In this case there are 3 inputs that receive the name of the student, the series(deggreId) and the class(classId). A button with the function to load the data and an edit that still doesn’t work.

  • And in each input you want a name, or all separated by commas?

1 answer

1


It is only necessary to do the parse once, and in your case was doing several times. As is a list of students you get can use a forEach to go through all and show the corresponding fields of each student so:

let students = JSON.parse(this.responseText);

for (let s of students){
    console.log(s.name + " " + s.degreeId + " " + s.classId);
}

Or if you want to use Arrow functions and literals template

let students = JSON.parse(this.responseText);

students.forEach(s => console.log(`${s.name} ${s.degreeId} ${s.classId}`));

Example:

let json = `[
{
    "id":1,
    "ra":12346,
    "name":"Pedro Santos Neves",
    "degreeId":1,
    "classId":1
},
{
    "id":2,
    "ra":456798,
    "name":"Maria Antônia Silva",
    "degreeId":2,
    "classId":1
},
{
    "id":3,
    "ra":752156,
    "name":"José Carlos do Prado",
    "degreeId":3,
    "classId":2
},
{
    "id":4,
    "ra":852348,
    "name":"Aparecido Costa Ribeiro",
    "degreeId":4,
    "classId":2
},
{
    "id":5,
    "ra":454643,
    "name":"Mariana Antunes Aguiar",
    "degreeId":6,
    "classId":2
},
{
    "id":6,
    "ra":192837,
    "name":"Ester Faria de Cassio",
    "degreeId":5,
    "classId":2
} ]`;

let students = JSON.parse(json);

students.forEach(s => console.log(`${s.name} ${s.degreeId} ${s.classId}`));

Edit

If I understood correctly in relation to html wants several <input>, 3 for each student.

You could then do so:

let json = `[
{
    "id":1,
    "ra":12346,
    "name":"Pedro Santos Neves",
    "degreeId":1,
    "classId":1
},
{
    "id":2,
    "ra":456798,
    "name":"Maria Antônia Silva",
    "degreeId":2,
    "classId":1
},
{
    "id":3,
    "ra":752156,
    "name":"José Carlos do Prado",
    "degreeId":3,
    "classId":2
},
{
    "id":4,
    "ra":852348,
    "name":"Aparecido Costa Ribeiro",
    "degreeId":4,
    "classId":2
},
{
    "id":5,
    "ra":454643,
    "name":"Mariana Antunes Aguiar",
    "degreeId":6,
    "classId":2
},
{
    "id":6,
    "ra":192837,
    "name":"Ester Faria de Cassio",
    "degreeId":5,
    "classId":2
} ]`;

let students = JSON.parse(json);
const formAlunos = document.querySelector(".col.l12.s12");
const divRow = formAlunos.querySelector(".row");

students.forEach(s => {
  let novo = divRow.cloneNode(true); //criar uma nova linha para o aluno
  let inputs = novo.querySelectorAll("input"); //buscar os inputs

  //atribuir os valores
  inputs[0].value = s.name;
  inputs[1].value = s.degreeId;
  inputs[2].value = s.classId;
  
  formAlunos.appendChild(novo); //adicionar a nova linha ao form
});

formAlunos.removeChild(divRow); //remover a primeira vazia
<div class="container">
  <form class="col l12 s12">
    <div class="row">
      <div class="input-field col l6 s7">
        <input placeholder="Aluno" id="aluno" type="text" class="validate">
        <label for="aluno"></label>
      </div>
      <div class="input-field col l3 s2">
        <input placeholder="Série" id="serie" type="text" class="center validate">
        <label for="serie"></label>
      </div>
      <div class="input-field col l3 s2">
        <input placeholder="Classe" id="classe" type="text" class="center validate">
        <label for="classe"></label>
      </div>
    </div>
  </form>
</div>

<div class="container">
  <div class="col s12">
    <div class="row">
      <a class="waves-effect waves-light btn left" onclick="loadDoc()">Carregar</a>
      <a class="waves-effect waves-light btn right">Editar</a>
    </div>
  </div>
</div>

  • Okay Isac worked on the console, but how do I load the data into the html inputs.

  • @Leandro When I answered I didn’t really have html. The goal is to create a <div class="row"> equal to what is for each student and add to the <form> with values in the right places ? Or in what way is the html stay after data is loaded

  • So Isac Html is in the question up there, under javascript, the idea here is to load the data into three inputs, understood, type Document.getElementById("student") and fill the input with the data of json understood.

  • @Leandro But you have 6 students in json. Load only the first ? Or how will it work?

  • Load them all, one under the other. Kind of loop and load all as it worked in the console only in the html inputs.

  • @Leandro I will try to be more precise in my question. After charging will be 18 <inputs> which is 3 per student ? Or will have 3 <input> in which the first is all the names, the second all the degrees, etc ?

  • Isac sent an image with the screen for you to better understand, have the 3 inputs next to each other in a right row, when I click the click button I would like to load the data of json one below the other as a list or a table.

  • @Leandro already edited. See if this was the result you were looking for.

  • Exactly Isac, this same I was looking for guy. Thank you!

Show 4 more comments

Browser other questions tagged

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