Android:在Android中为应用程序离线存储JSON数据的最佳方法是什么?

Mr *_*ker 12 android json

我想实现一个应用程序,它将显示路线编号,骑手已登陆谷歌地图.因此,要查找路由号,我需要一些有关路由的信息,这些信息在JSON文件中定义.JSON文件中的信息将从应用程序中脱机处理和解析.它也应该与应用程序一起安装和卸载.JSON文件包含100多个route_direction.它看起来像下面的代码.我可以在哪个文件夹中存储JSON文件到我的Android项目中?存储这样的数据的最佳方法是什么?

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} 

                    }


                }

        },........

   ]

}
Run Code Online (Sandbox Code Playgroud)

Dar*_*ski 13

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

将json保存到SharedPreferences:

    PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("theJson",jsonObject.toString()).apply();
Run Code Online (Sandbox Code Playgroud)

获取存储的json:

JsonObject jsonObject = PreferenceManager.
getDefaultSharedPreferences(this).getString("theJson","");
Run Code Online (Sandbox Code Playgroud)

  • 如果需要存储少量数据,请使用共享首选项.参考:[Android-Storage](https://developer.android.com/training/basics/data-storage/index.html) (3认同)