2014-02-28 101 views
0

我下面Micers回答store and retrieve a class object in shared preference檢索的String []從JSONObject的從sharedPreferences

但我怎麼檢索回來的String []從JSONObject的

我的課是:

public class AppearExamState implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    Boolean flag = false; 
    int questionId; 
    int subjectId; 
    String[] storedAnswers; 

    public AppearExamState(int q, int subjId, String[] ans, boolean flg){ 
     questionId = q; 
     subjectId = subjId; 
     storedAnswers = ans; 
     flag = flg; 
    } ....... 
.........getters and setters..... 

....... 
//----------------------------------------------------------------- 
// Here I convert myObject to JsonObject 
//----------------------------------------------------------------- 
    public JSONObject getJSONObject() { 
     JSONObject obj = new JSONObject(); 
     try { 
      obj.put("Qid", this.questionId); 
      obj.put("Stored_Ans", this.storedAnswers);// is this RIGHT?? 
      obj.put("subj_id", this.subjectId); 
      obj.put("Flag", this.flag); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return obj; 
    } 

現在,當我在sharepref存儲我遵循老鼠答案,並做到這一點:

//-------------------------------------------------------------------------------------- 

    public void sendSharedPreference(ArrayList<AppearExamState> arrayl){ 
     SharedPreferences mPrefs = this.getSharedPreferences("aimmds_state", 0); 
     SharedPreferences.Editor prefsEditor = mPrefs.edit(); 
     Set<String> set= new HashSet<String>(); 
     for (int i = 0; i < arrayl.size(); i++) { 
      set.add(arrayl.get(i).getJSONObject().toString()); 
     } 
     prefsEditor.putStringSet("aimmds_state", set); 
     prefsEditor.commit(); 

     } 
//-------------------------------------------------------------------------------------- 

和我在這裏堅持了不知道如何做到這一點

public ArrayList<AppearExamState> loadFromStorage(Context c) { 
     SharedPreferences mPrefs = c.getSharedPreferences("aimmds_state", 0); 

     ArrayList<AppearExamState> items = new ArrayList<AppearExamState>(); 

     Set<String> set = mPrefs.getStringSet("aimmds_state", null); 
     for (String s : set) { 
      try { 
       JSONObject jsonObject = new JSONObject(s); 
       int Quesid = jsonObject.getInt("Qid"); 
       int SubjId = jsonObject.getInt("subj_id"); 
       Boolean flag = jsonObject.getBoolean("Flag"); 
       String[] StoAnswer = ????? ; 

       AppearExamState myclass = new AppearExamState(Quesid, SubjId, StoAnswer, flag); 

       items.add(myclass); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     return items; 
    } 

回答

1

你不能把一個數組JSON,但你可以把一個Collection,這是由JSONArrayhttp://www.json.org/javadoc/org/json/JSONObject.html#put%28java.lang.String,%20java.util.Collection%29)表示。所以,正確的方法把你的數組JSONObject是:

obj.put("Stored_Ans", new JSONArray(this.storedAnswers)); 

,並獲得:

JSONArray jsonArray = jsonObject.getJSONArray("Stored_Ans"); 
String[] StoAnswer = new String[jsonArray.length()]; 
for(i = 0; i < jsonArray.length(); i++){ 
    StoAnswer[i] = jsonArray.getString(i); 
}