Can I use JSON to save Arraylists in Shared Preferences?

Asked

Viewed 134 times

0

I’m developing an app on a college project and I’m creating the control part, I won’t mess with the server part for now because I don’t know much about.

I’m creating functions to generate some Arrays with the items of my application so that I can do tests, only that whenever the application is closed, this data is lost. I was reading about the Shared Preferences, which can be used to store small amounts of data, and I saw that I can use JSON to create Arrays.

My question is whether it will really solve my problem and whether it is a good idea to do so. I had thought about storing it in a file but my advisor had already mentioned that we would use JSON to exchange data with the server, so I guess it would not be work thrown away.

1 answer

1


You can use JSON to save Arraylists using, for example, Jsonarray (https://developer.android.com/reference/org/json/JSONArray.html), has an example in the international forum.

https://stackoverflow.com/questions/17037340/converting-jsonarray-to-arraylist

ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.get(i).toString());
   } 
} 

But it is worth noting that from API 11 it is possible to use Stringsets, as both are collections, depending on your need, this manipulation can be easier.

https://developer.android.com/reference/android/content/SharedPreferences.Editor.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.