0

I was following Micers Answer store and retrieve a class object in shared preference

But how do I retrieve back STring[] from JSOnObject

my class is :

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;
    }

Now when I store in sharepref I followed micers answer and did this:

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

    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();

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

and I am stuck here with no clue how to do this

 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 Answer 1

1

You cannot put an array to JSON, but you can put a Collection, which is represented by a JSONArray (http://www.json.org/javadoc/org/json/JSONObject.html#put%28java.lang.String,%20java.util.Collection%29). So the correct way to put your array to JSONObject is:

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

and to get:

JSONArray jsonArray = jsonObject.getJSONArray("Stored_Ans");
String[] StoAnswer = new String[jsonArray.length()];
for(i = 0; i < jsonArray.length(); i++){
    StoAnswer[i] = jsonArray.getString(i);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.