在Android商店对象对象、商店、Android

2023-09-12 09:50:43 作者:微信扫描或直接搜索关注“腾牛个性网”微信公众号,方便随时查看

我想知道什么是存储,我在我的应用程序自定义的Java对象的最佳选择。我读过有关序列化,但它似乎是相当缓慢的。

I would like to know what would be the best option to store a custom Java object that I have in my application. I've read about serialization but it seems to be quite slow.

什么是最好的选择吗?

推荐答案

如果你不存放过大量的数据,您可以使用GSON到Java对象转换为一个JSON字符串,字符串存储在您的应用程序preferences。 GSON拥有的toJSON(OBJ)法和签证可见。

If you're not storing an over abundance of data you can use Gson to convert a Java object to a Json string and store the string in your app preferences. Gson has a toJson(obj) method and visa-vis.

获取数据出应用程序preferences并成一个对象:

Getting data out of app preferences and into an object:

String json = appSharedPrefs.getString("someName","");    
return gson.fromJson(json, YourModel.class);

要得到你的对象数据到应​​用程序preferences:

To get data from your object into app preferences:

String json = gson.toJson(obj);

然后 prefsEditor.putString(someName,JSON)。适用()