2
I’m having trouble turning a JSON into a Java list, I tried with GSON and Jackson, but I couldn’t succeed with either. My JSON is formatted like this:
[[["00000000000","00000","Fulano de Souza",""],
["11111111111","111111","Sicrano da Silva",""],["22222222222","","Cilano de Tal","X"]]]
This is my class for JSON:
@JsonDeserialize(using= Deserializador.class)
public class AlunoSGP {
@JsonProperty("0")
private String nomeAluno;
@JsonProperty("1")
private String cpfAluno;
@JsonProperty("2")
private String raAluno;
@JsonProperty("3")
private boolean bolsista;
public AlunoSGP(String nome, String ra, String cpf, String bolsa) {
this.nomeAluno = nome;
this.raAluno = ra;
this.cpfAluno = cpf;
bolsista = "X".equals(bolsa);
}
//Getters e Setters
And my code that deserializes JSON is like this:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(AlunoSGP.class, new Deserializador());
try{
List<AlunoSGP> lstAlunos = Arrays.asList(mapper.readValue(json, AlunoSGP[].class));
System.out.println("\nTeste lista");
for(AlunoSGP aluno : lstAlunos){
System.out.println(aluno.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
Finally, this is my code for Deserializador
customized:
@Override
public AlunoSGP deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
TreeNode tn = p.readValueAsTree();
String nomeAluno;
String raAluno;
String cpfAluno;
String bolsaAluno;
if (tn.get(0) != null) {
cpfAluno = tn.get(0).toString();
} else {
cpfAluno = "Erro nomeAluno";
}
if (tn.get(1) != null) {
raAluno = tn.get(1).toString();
} else {
raAluno = "Erro nomeAluno";
}
if (tn.get(2) != null) {
nomeAluno = tn.get(2).toString();
} else {
nomeAluno = "Erro nomeAluno";
}
if (tn.get(3) != null) {
bolsaAluno = tn.get(3).toString();
} else {
bolsaAluno = "Erro nomeAluno";
}
return new AlunoSGP(nomeAluno, raAluno, cpfAluno, bolsaAluno);
}
With this code I can print the Students on my list, but the data is all Erro nomeAluno
, that is, my deserializer is returning null
in JSON positions, I am totally lost on what to do.
As far as I know, JSON is bounded by
{...}
and not[...]
, besides"terem": "valores"
.– CypherPotato
@Cypherpotato, there is the JSON list and the JSON object. A JSON object is delimited by
{}
and is composed of key/value pairs, where the key is a string and a value is an entity. A JSON list is delimited by[]
and contains several entities. An entity, in this case, may be an object, a list, or a scalar (such as null, number, string, boolean, or any other that I may have forgotten)– Jefferson Quesado
@Cypherpotato, for this case it seems that the most appropriate would be to produce and consume JSON objects (or list of objects, if it can return more than one). Makes more sense than sending arbitrary lists up and down...
– Jefferson Quesado
@Cypherpotato Complementing Jefferson’s commentary, any of the types defined by JSON syntax is considered valid (i.e., only the string
"abc"
or the number123
, alone, they are also valid JSON’s). What happens is that 99.999% of Apis return objects (delimited by{}
), but nothing prevents the return of a list (delimited by[]
), or even a simple string or number... Inclusive, depending on the case, is even better. If the API is "api.com/users/qtd_total", why not just return100
instead of{ "qtd_total_users": 100 }
? :-)– hkotsubo
Probable duplicity https://answall.com/questions/329595/ler-json-e-transforma-em-lista-java Here are some answers that might help you.
– Leandro Santos