Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save json array to android internal storage

Tags:

android

So I'm working on a project like this http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ which let's me to display my database's values to my android application GUI.

I need to save the database values I need to the android internal storage so I can access it even if my application is not connected to the server. Any help?

Thank you

like image 864
user2377651 Avatar asked Jun 16 '13 10:06

user2377651


People also ask

Where do I put JSON files on Android?

json file is generally placed in the app/ directory (at the root of the Android Studio app module).

How do you store an array of JSON objects?

If it is just an array you want to store and not store a name attribute to it. You can just store it as [1,2,3,4,5,6] . For example if you are storing it in an RDBMS, you may name the column as array and store the value as a JSON array. OR is wrapped in square brackets in case of arrays (ordered list of values).

Can you store an array in a JSON file?

JSON array can store string , number , boolean , object or other array inside JSON array. In JSON array, values must be separated by comma. Arrays in JSON are almost the same as arrays in JavaScript.


2 Answers

You can write whole json as string in shared prefernces, and then get it and parse it to display in GUI even when device is offline:

String str = json.toString();
SharedPreferences sharedPref = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE );
SharedPreferences.Editor prefEditor = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE ).edit();
prefEditor.putString( "json", str );
prefEditor.commit();
like image 185
Aneeq Anwar Avatar answered Oct 03 '22 03:10

Aneeq Anwar


If you chose local sqlite database as your solution, I wrote a lightweight utility for saving your json into a sqlite.

checkout git repo here. https://github.com/wenchaojiang/JSQL

You only need one line of code.

new JSQLite(yourJsonString, yourDB).persist();

Hope it helps

like image 29
GingerJim Avatar answered Oct 03 '22 05:10

GingerJim