Home > Back-end >  Trying to write a json file using an Object with an arraylist
Trying to write a json file using an Object with an arraylist

Time:10-16

I have a class called "Estudante" and I'm trying to write a file in json using a object of the class "Estudante". My problem is, when I try to write the value to json, the strings and ints are written but the arraylist isn't written in the file.

heres the code:

package org.example;

import java.util.ArrayList;
import java.util.Objects;

public class Estudante {
    //attributes of the class Estudante
    private int nrEstudante;
    private String nome;
    private int idade;
    private ArrayList<String> unidadesCurriculares;

    //default constructor (good practice)

    public Estudante() {

    }
    public Estudante(int nrEstudante, String nome, int idade, ArrayList<String>unidadesCurriculares) {
        this.nrEstudante = nrEstudante;
        this.nome = nome;
        this.idade = idade;
        this.unidadesCurriculares = unidadesCurriculares;
    }

    public int getNrEstudante() {
        return nrEstudante;
    }

    public void setNrEstudante(int nrEstudante) {
        this.nrEstudante = nrEstudante;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }
}

Next I have my Main Class where I try to make the file in json with the data from the new object

package org.example;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        ArrayList<String> ucurriculares = new ArrayList<String>();
        ucurriculares.add("ingles");
        ucurriculares.add("Espanhol");
        //ArrayList<Object> unidade = new ArrayList<Object>();
        //unidade.add(ucurriculares);

        Estudante estudante;
        estudante = new Estudante(1234,"Student",16,ucurriculares);
        try{

            ObjectMapper mapper = new ObjectMapper();
            String StrJson = mapper.writeValueAsString(estudante);
            mapper.writeValue(new File("estudante.json"),StrJson);
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }

    }
}

This is the current output of my file:

"{\"nrEstudante\":1234,\"nome\":\"Student\",\"idade\":16}" 

Turns out I forgot to make setters and getters for the ArrayList

CodePudding user response:

You can try any of these two approaches with your existing code:

Approach 1: Create getters and setters for all the attributes of the Estudante.java class

Approach 2: Use mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); on the ObjectMapper.(No need to create getter and setter)

For Approach 2 : As I have used ObjectMapper from different package,

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

Need to add this dependency in pom.xml

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.4</version>
        </dependency>

Output in the file with both approaches:

"{\"nrEstudante\":1234,\"nome\":\"Student\",\"idade\":16,\"unidadesCurriculares\":[\"ingles\",\"Espanhol\"]}"
  • Related