Je suis assez nouveau à la programmation. Et j'essaye de sauver mes objets dans un txt avec JSON. Mais j'ai un problème lorsque j'essaie de passer l'objet JSON à mon constructeur.Comment faire pour convertir un attribut ArrayList <String> ou ArrayList <Integer> ou ArrayList <MYCLASS> d'un objet JSON à un objet Java?
J'ai cette classe.
public class Aluno {
protected String matricula;
protected ArrayList<Integer> concluidas;
protected ArrayList<Integer> cursando;
protected ArrayList<Notas> notas;
Et j'utiliser cette méthode pour convertir en JSON
public JSONObject toJson(){
JSONObject json = new JSONObject();
json.put("matricula",this.matricula);
json.put("concluidas",this.concluidas);
json.put("notas",this.notas);
return json; }
Le problème est à mon constructeur:
public Aluno(JSONObject json) {
this.matricula = json.getString("matricula");
this.concluidas = (ArrayList<Integer>) json.get("concluidas");
this.notas = (ArrayList<Notas>) json.get("notas");
this.cursando = (ArrayList<Integer>) json.get("cursando");
}
Je reçois des erreurs ici
this.concluidas = (ArrayList<Integer>) json.get("concluidas");
erreur : Exception dans le fil "principal" java.l ang.ClassCastException: json.JSONArray ne peut pas être converti en java.util.ArrayList
Idem pour les notations ArrayList cursando et ArrayList.
Où avez-vous créé JSON Array? –