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 Arrayはどこに作成しましたか? –