My app currently reads in a JSON string from an external datasource and parses it into a HashMap of objects. This happens every 10 seconds while the app is open.

I want to implement a cache for that object so that if the user is offline, the last retrieved dataset can still be displayed.

What is the best way to do that? I thought of a few but I'm not sure what's the best way.

  1. Write JSON string to internal storage on every data fetch

  2. Write JSON string to internal storage when app closes

  3. Serialize object and write to internal storage (how?)

有帮助吗?

解决方案

I think Serialize will be the best way to do this. Just create one class whose attributes will be the JSON keys. As for example if your JSON is like

   {
     Car:{
     name: "XYZ"
     color: "red"
     }
    }

Your class will be:

    class Car implements Serializable{
     private String name;
     private String color;
    }

Check this link to know how to serialize and deserialize object.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top