Question

I'm creating my first Android application, and utilizing the Gson library to convert some custom objects to strings and back. I can convert an individual custom object back and forth easily, but run into trouble when attempting to convert a List of custom objects.

I know this probably isn't the most practical usage, but it's a short term solution. Here is the code:

List<CarObj>cars = dealership.getCar(); // grabbing the cars list from another object
String carsListString = gson.toJson(cars); // convert to json string

This won't compile....says it's expecting an expression. I've got the class definition parameter, and the json string, but I don't know what's missing:

List<CarObj>theCars = gson.fromJson(List<CarObj>, carListString);

Any hints are greatly appreciated.

Was it helpful?

Solution

Try something like this:

List<CarObj> cars = gson.fromJson(carsListString, new TypeToken<List<CarObj>>(){}.getType());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top