user927341
user927341

Reputation:

Is it possible to store Map<String,Object> in sharedPeference without converting to json?

I am trying to store a hash map in shared preference which contains the key as a string and value an object. The json conversion of my class is

{"avlStatus":1,"isOnline":false}

Upvotes: 0

Views: 151

Answers (2)

Zack Matthews
Zack Matthews

Reputation: 409

SharedPreferences probably isn't the best solution if you want to store a structure. However you could always write private data to internal storage. Google's Data Storage doc shows the following implementation of writing private data to internal storage:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

To retrieve your file simply call openFileInput() and pass it the name of the file to read. This returns a FileInputStream. Read bytes from the file with read(). Then close the stream with close().

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007265

You are welcome to convert it to XML instead of JSON. Or some other string notation. However, you cannot store arbitrary data structures in SharedPreferences.

Upvotes: 3

Related Questions