Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: what is the best way to store JSON data offline for the app in android?

Tags:

json

android

I want to implement an app, that will show the route number, the rider has boarded in google map. So to find the route number I need some information about the routes, which are defined in the JSON file. The information in the JSON file will be proccessed and parsed offline from the app. It should be installed and uninstall with the app too. The JSON file contains more than 100 route_direction. It will looks something like the code below. In which folder can I store the JSON file in my Android project? And what is the best way to store data like this?

JSON file:

{"route_direction":[
   {
     1_ab:{
                     
                    { 
                      stopsName_arrivalTime:{"abc":{"mon-fri":[05:33,06:00,06:30,...],
                                                        "sat":[],
                                                        "son":[]
                                                               }
                                             }, 

                                             
                         stops_loca:{lat:53.337994, long:10.600423} ,

                    },
                    { 
                      stopsName_arrivalTime:{"def":{"mon-fri":[],
                                                        "sat":[],
                                                        "son":[]
                                                         }
                                             }, 

                                             
                         stops_loca:{lat:53.740624, long:10.101322} 

                    },
                    { 
                      stopsName_arrivalTime:{"hgk":{"mon-fri":[],
                                                        "sat":[],
                                                        "son":[]
                                                               }
                                             }, 

                                             
                         stops_loca:{lat:53.644834, long:10.203372} 

                    }



                }

       },

        {1_ba:{
                     
                    { 
                      stopsName_arrivalTime:{"hgk":{"mon-fri":[05:33,06:00,06:30],
                                                        "sat":[],
                                                        "son":[]
                                                               }
                                             }, 

                                             
                         stops_loca:{lat:53.437994, long:10.400423} ,

                    },
                    { 
                      stopsName_arrivalTime:{"def":{"mon-fri":[],
                                                        "sat":[],
                                                        "son":[]
                                                         }
                                             }, 

                                             
                         stops_loca:{lat:53.540624, long:10.601322} 

                    },
                    { 
                      stopsName_arrivalTime:{"abc":{"mon-fri":[],
                                                        "sat":[],
                                                        "son":[]
                                                               }
                                             }, 

                                             
                         stops_loca:{lat:53.244834, long:10.703372} 

                    }


                }

        },........

   ]

}
like image 768
Mr Asker Avatar asked Apr 13 '15 13:04

Mr Asker


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).

Is JSON the best way to store data?

JSON is perfect for storing temporary data that's consumed by the entity that creates the data. A good example is user-generated data such as filling out a form or information exchange between an API and an app.

Can JSON be used in mobile application?

Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.

What is JSON array in android?

JSON (Javascript Object Notation) is a programming language . It is minimal, textual, and a subset of JavaScript. It is an alternative to XML. Android provides support to parse the JSON object and array.


2 Answers

Maybe you can save them using SharedPreferences. You can store a json object/array by doing:

Saving json to SharedPreferences:

    PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("theJson",jsonObject.toString()).apply();

Getting the stored json:

JsonObject jsonObject = PreferenceManager.
getDefaultSharedPreferences(this).getString("theJson","");
like image 78
Darko Petkovski Avatar answered Oct 18 '22 11:10

Darko Petkovski


Basically, there are two places where raw stuff may be stored: assets/ and res/raw/ (raw resources). These have been discussed a lot, for example, see the links:

The reason for Assets and Raw Resources in Android

http://thedevelopersinfo.com/2009/11/27/using-files-as-raw-resources-in-android/

http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders

like image 2
18446744073709551615 Avatar answered Oct 18 '22 12:10

18446744073709551615