14

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} 

                    }


                }

        },........

   ]

}
Mr Asker
  • 2,300
  • 11
  • 31
  • 56
  • 3
    You should convert it to an object and store in an sqlite database. If you still want to store it as a file use the assets folder. – Jorge Campos Apr 13 '15 at 13:24
  • @Jorge Campos: How can I convert the JSON file to an object? – Mr Asker Apr 13 '15 at 21:45
  • Take a look at this thread: http://stackoverflow.com/questions/6079505/converting-json-string-to-java-object Note that the answer that mention GSon is using an external library from google. – Jorge Campos Apr 13 '15 at 23:16
  • @Jorge what is the taken advantage from store the data in the sqlite? so I mean after converting it to object I thought I can access the data from there without storing again? – Mr Asker Apr 14 '15 at 09:06
  • The advantages are that you would have a more concise data stored. That way if you need to get specific information from that data you would do it if sql code. From it you could make your application more responsive with report data etc. But of course, that is only if think about scalability of your application if your requirements is about just store it and retrieve it a simple file on the assets will do the job just fine. – Jorge Campos Apr 14 '15 at 11:40
  • @JorgeCampos: I don't think use of sqlite is the right approach for this problem. The thread you linked to seems to agree with me as it just handles the json data and makes no mention of using SQL – IcedDante Feb 18 '17 at 03:56

3 Answers3

16

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","");
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • if you notice I have complex JSON string. can I do that with sharedPreferneces too? – Mr Asker Apr 14 '15 at 07:11
  • @MrAsker sure, you can save a complex or large without any problem. If you choose to it with using sharedPreferences (and if you are saving the json as a string) just be carefull with the string formatting. Hope it helps! – Darko Petkovski Apr 14 '15 at 08:01
  • 3
    Use shared preferences if you need to store small amounts of data. Ref: [Android-Storage](https://developer.android.com/training/basics/data-storage/index.html) – Rohit Reddy Abbadi Oct 01 '17 at 12:25
  • in this situation using Hawk https://github.com/orhanobut/hawk is better than using sharepreferences – smrf Jul 29 '20 at 05:39
2

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

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

You store abitrary files that you like to package with your app in the assets folder

https://developer.android.com/tools/projects/index.html

main/assets/

This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

See this question on how to read from assets.

Community
  • 1
  • 1
Patrick
  • 33,984
  • 10
  • 106
  • 126
  • 2
    Know this is an old post but be careful with this solution. Do not store any sensitive information in the asset folder. It folder can be read (and altered) by a savvy user. – MarcJames Feb 21 '17 at 15:12