Using the Gson library, how do I convert a JSON string to an ArrayList of a custom class JsonLog? Basically, JsonLog is an interface implemented by different kinds of logs made by my Android app–SMS logs, call logs, data logs–and this ArrayList is a collection of all of them. I keep getting an error in line 6.
public static void log(File destination, JsonLog log) {
Collection<JsonLog> logs = null;
if (destination.exists()) {
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader(destination));
logs = gson.fromJson(br, ArrayList<JsonLog>.class); // line 6
// logs.add(log);
// serialize "logs" again
}
}
It seems the compiler doesn’t understand I’m referring to a typed ArrayList. What do I do?
You may use TypeToken to load the json string into a custom object.
Documentation:
Kotlin:
If you need to do it in Kotlin you can do it like this:
Or you can see this answer for various alternatives.