0
我很新的编程。 而我试图用JSON将我的对象保存到txt中。 但我有一个问题,当我尝试将JSON对象传递给我的构造函数。如何将属性ArrayList <String>或ArrayList <Integer>或ArrayList <MYCLASS>从JSON对象转换为java对象?
我有这堂课。
public class Aluno {
protected String matricula;
protected ArrayList<Integer> concluidas;
protected ArrayList<Integer> cursando;
protected ArrayList<Notas> notas;
而且我用这个方法把它转换为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; }
的问题是在我的构造函数:
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");
}
我得到的错误,在这里
this.concluidas = (ArrayList<Integer>) json.get("concluidas");
ERROR :线程“main”java.l中的异常ang.ClassCastException:json.JSONArray不能转换为java.util.ArrayList
对于ArrayList cursando和ArrayList notas也是如此。
你在哪里创建JSON数组? –