Insert Json into Json

0

In a client-server structure (in JAVA), I have the following Json structures separately (example): Structure of Students :

{"idAluno":1,"nomeAluno":"Teste da Silva","listaDeTurmas":[1,2]}

Structure of Classes :

{"idTurma":1,"nomeTurma":"Redes"}
{"idTurma":2,"nomeTurma":"Compiladores"}

Each structure is managed by a different Java application. Another application makes requests to each of them. After a request to the Students application, I receive the structure I referenced above as a response. In the case of a search request, I need to display the data of the class in the student's Array of classes. Example:

{ 
    "idAluno": 1,
    "nomeAluno": "Teste da Silva",
    "listaDeTurmas": [
        {
            "idTurma": 1,
            "nomeTurma": "Redes"
        },
        {
            "idTurma": 2,
            "nomeTurma": "Compiladores"
        }
    ]
}

For each class id, I make a request in the Class application and receive the corresponding class structure. But what is the correct way to modify the Array in the student's Json and cause the class data to be displayed instead of simply its code (as shown above)? Do I need to break down the two structures and create a new Json from there or is there a simpler way to do this?

    
asked by anonymous 12.04.2018 / 16:18

1 answer

3

An idea for the solution:

  • Performs the Student JSON request and mounts it within a Student object
  • Performs the requests of the Classes, according to the ids you already have.
  • Mounts a Class object according to what it receives in the class JSON request.
  • Creates a new field in the Student object of type ArrayList<Turma> and passes all objects that you seek from Class to this array.
  • Generates the new JSON from Student, with the list of Classes already added to the object.
  • Classes

    Attention here: Only create the set method of the int[] listaDeTurmas variable in the Student class so that it does not play its array of int[] when you export the class with the classes later

    public class Aluno {
    
      private int idAluno;
      private String nomeAluno;
      private int[] listaDeTurmas;
    
      private List<Turma> listaTurma = new ArrayList<>();
    
      public void setListaDeTurmas(int[] listaDeTurmas) {
          this.listaDeTurmas = listaDeTurmas;
      }
      //metodo para adicionar elemento a sua List
      public void addTurma(Turma t) {
          listaTurma.add(t);
      }
      //gets e sets das outras três variáveis...
    } 
    
    public class Turma {
    
      private int idTurma;
      private String nomeTurma;
    
      //gets e sets...
    }
    

    To reproduce and facilitate the process, I used the Jackson library to break JSON

     import com.fasterxml.jackson.databind.ObjectMapper;
     import java.io.IOException;
    
     public static void main(String[] args)  {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String alunoString = "{\"idAluno\":1,\"nomeAluno\":\"Teste da Silva\",\"listaDeTurmas\":[1,2]}";
            Aluno aluno = mapper.readValue(alunoString, Aluno.class);
    
            String turma1String = "{\"idTurma\":1,\"nomeTurma\":\"Redes\"}";
            Turma turma1 = mapper.readValue(turma1String, Turma.class);
    
            String turma2String = "{\"idTurma\":2,\"nomeTurma\":\"Compiladores\"}";
            Turma turma2 = mapper.readValue(turma2String, Turma.class);
    
            aluno.addTurma(turma1);
            aluno.addTurma(turma2);
    
            String jsonAlunoCompleto = mapper.writeValueAsString(aluno);
            System.out.println(jsonAlunoCompleto);
            //{"idAluno":1,"nomeAluno":"Teste da Silva","listaTurma":[{"idTurma":1,"nomeTurma":"Redes"},{"idTurma":2,"nomeTurma":"Compiladores"}]}
        } catch (IOException e) {
            System.out.println(e.getCause());
        }
    
    }
    
        
    12.04.2018 / 17:26