Insert Json within Json

Asked

Viewed 1,911 times

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 requests each of them. After a request to the Students, I receive as a response the structure I referenced above. In the case of a search request, I need to display the data of the class in question in the Class Array. 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 Classes application and receive the corresponding structure of the searched class. But what is the correct way to modify the Array in the student’s Json and have the class data displayed instead of simply its code (as the example above)? I need to dismember the two structures and create a new Json from there or there’s a simpler way to do this?

  • I would pass the JSONs for matching objects in the application that is making the request, would organize the list of Class objects inside the Student object, and then reassemble the string JSON from the object Student. (only one of the ways of solution) :p

  • Only that the Class Array of my Student object is an Integer Arraylist. I would have to create another class with String Arraylist from there?

1 answer

3


An idea for the solution:

  1. Make the Student JSON request and mount it inside a Student object
  2. Make the requisition of Classes, according to the ids you already have.
  3. Assemble a Class object according to what you receive in the class JSON request.
  4. Creates a new field in the Student object of type ArrayList<Turma> and passes all the objects you search from Class to that array.
  5. Generates the new JSON from Student, with the list of Classes already added to the object.

Classes

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

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 play and facilitate the process I used the library Jackson to break the 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());
    }

}
  • I’m having an error trying to convert the student string to the Student class: The type com.fasterxml.Jackson.core.Jsonparser cannot be resolved. It is Indirectly referenced from required . class files

  • Actually I had been using the Gson library until then, and had implemented something similar before you edit the comment. I created a Arraylist<Class> Classes in the object Student and then do the same thing you suggested, but using the methods toJson and fromJson for String <-> Class conversions (even the parameters passed are the same). But the answer ends up coming out in the same format (only with class ids).

  • But I’m thinking that the problem is not in this part of logic....

  • To resolve the first Jsonparser error, simply add these libs

  • To solve the problem of it returning the same variables, make sure you added the method set to rescue the ArrayList class

  • Yes, the set was created. And I left only the set of the other Array (in my case of Integer, which contains only the Ids), as you said.

  • I fixed that first bug with the libs (I had only included the databind). But I executed your code and the jsonAlunoCompleto comes out as null.

  • You will need to debug and see which object is starting the error, if it is the mapper that cannot transform, or if it is some object Student or Class that is null. Besides null, JVM launches some Exception?

Show 3 more comments

Browser other questions tagged

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