2017-10-15 15 views
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と同じです。

+0

JSON Arrayはどこに作成しましたか? –

答えて

0
ArrayList<Integer> concluidas = new ArrayList<Integer>();  
JSONArray jArray = (JSONArray)json.get("concluidas");; 
if (jArray != null) { 
    for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getInt(i)); 
    } 
} 

他のJSONArrayも同様に変換できます。

関連する問題