Android:在Android中,什么是离线存储JSON数据的最佳方式?

时间:2022-12-08 13:11:32

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?

我想实现一个app,它会显示路线号码,车手已经在谷歌地图上登陆。为了找到路线编号,我需要一些关于路线的信息,这些信息是在JSON文件中定义的。JSON文件中的信息将从app中进行离线处理和解析,并与app一起进行安装和卸载。JSON文件包含100多个route_direction。它看起来像下面的代码。我可以将JSON文件存储在哪个文件夹中?像这样存储数据的最好方法是什么?

JSON file:

JSON文件:

{"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} 

                    }


                }

        },........

   ]

}

3 个解决方案

#1


12  

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

也许您可以使用SharedPreferences保存它们。可以通过以下操作存储json对象/数组:

Saving json to SharedPreferences:

储蓄json SharedPreferences:

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

Getting the stored json:

存储json:

JsonObject jsonObject = PreferenceManager.
getDefaultSharedPreferences(this).getString("theJson","");

#2


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:

基本上,有两个地方可以存储原始数据:资产/和res/raw/(原始资源)。这些已经被讨论了很多,例如,看链接:

The reason for Assets and Raw Resources in Android

Android的资产和原始资源的原因。

http://thedevelopersinfo.com/2009/11/27/using-files-as-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

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

#3


0  

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

你可以将你喜欢与你的应用程序一起打包的abitrary文件存储在assets文件夹中

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

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.

这是空的。您可以使用它来存储原始资产文件。您在这里保存的文件被编译成。apk文件,并且原始文件名被保留。您可以像使用uri的典型文件系统一样导航这个目录,并使用AssetManager将文件作为字节流进行读取。例如,对于纹理和游戏数据来说,这是一个很好的位置。

See this question on how to read from assets.

请参阅关于如何从资产中读取的问题。

#1


12  

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

也许您可以使用SharedPreferences保存它们。可以通过以下操作存储json对象/数组:

Saving json to SharedPreferences:

储蓄json SharedPreferences:

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

Getting the stored json:

存储json:

JsonObject jsonObject = PreferenceManager.
getDefaultSharedPreferences(this).getString("theJson","");

#2


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:

基本上,有两个地方可以存储原始数据:资产/和res/raw/(原始资源)。这些已经被讨论了很多,例如,看链接:

The reason for Assets and Raw Resources in Android

Android的资产和原始资源的原因。

http://thedevelopersinfo.com/2009/11/27/using-files-as-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

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

#3


0  

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

你可以将你喜欢与你的应用程序一起打包的abitrary文件存储在assets文件夹中

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

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.

这是空的。您可以使用它来存储原始资产文件。您在这里保存的文件被编译成。apk文件,并且原始文件名被保留。您可以像使用uri的典型文件系统一样导航这个目录,并使用AssetManager将文件作为字节流进行读取。例如,对于纹理和游戏数据来说,这是一个很好的位置。

See this question on how to read from assets.

请参阅关于如何从资产中读取的问题。