3

I have an application that gets an api key and account name from a webservice, I am storing this apikey and account name for further use(multiple accounts and apikeys).

because you can only store primitive types in the sharedPreferences I parse the JSONArray toString. In another part of the application the user must be able to remove an account from his app. So I retrieve the string and Parse it back to an JSONArray.

how do I remove an JSONObject from the array and save it so I can parse it back to an string and save it again?

2 Answers 2

6

You should convert it in arraylist and remove object and create jsonarray,,,

ArrayList<String> list = new ArrayList<String>();
//First Position remove
list.remove(0);
JSONArray jsArray = new JSONArray(list);
Sign up to request clarification or add additional context in comments.

1 Comment

min SDK for remove() method is 19
3

i use:

public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) {

JSONArray Njarray=new JSONArray();
try{
for(int i=0;i<jarray.length();i++){     
    if(i!=pos)
        Njarray.put(jarray.get(i));     
}
}catch (Exception e){e.printStackTrace();}
return Njarray;

}

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.