Can I use JSON to save ArrayLists to Shared Preferences?

0

I'm developing an application in a college project and I'm creating the control part, I will not mess with the server part for now because I do not know much about it.

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

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

asked by anonymous 28.10.2016 / 22:18

1 answer

1

You can use JSON to save ArrayLists, for example by using JSONArray ( link ), has an example in the international forum.

  

link

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 mentioning that from API 11 it is possible to use StringSets, since both are collections, depending on your need, this manipulation may be easier.

  

link

    
29.10.2016 / 00:05