문제

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.

도움이 되었습니까?

해결책

Try something like this:

List<CarObj> cars = gson.fromJson(carsListString, new TypeToken<List<CarObj>>(){}.getType());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top