Android:在Android中离线存储JSON数据的最佳方法是什么? [英] Android: what is the best way to store JSON data offline for the app in android?

查看:99
本文介绍了Android:在Android中离线存储JSON数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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文件:

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

                    }


                }

        },........

   ]

}

推荐答案

也许您可以使用共享首选项.您可以执行以下操作来存储json对象/数组:

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

将json保存到SharedPreferences:

Saving json to SharedPreferences:

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

获取存储的json:

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

这篇关于Android:在Android中离线存储JSON数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆