Gson имеет некоторые ограничения в отношении коллекций из-за стирания типов Java. Вы можете прочитать больше об этом здесь .
Из вашего вопроса я вижу, что вы используете ArrayList
и LinkedList
. Вы уверены, что не хотели использовать только List
, интерфейс?
Этот код работает:
List<String> listOfStrings = new ArrayList<String>();
listOfStrings.add("one");
listOfStrings.add("two");
Gson gson = new Gson();
String json = gson.toJson(listOfStrings);
System.out.println(json);
Type type = new TypeToken<Collection<String>>(){}.getType();
List<String> fromJson = gson.fromJson(json, type);
System.out.println(fromJson);
Обновление : я изменил ваш класс на этот, поэтому мне не нужно возиться с другими классами:
class IndicesAndWeightsParams {
public List<Integer> indicesParams;
public List<String> weightsParams;
public IndicesAndWeightsParams() {
indicesParams = new ArrayList<Integer>();
weightsParams = new ArrayList<String>();
}
public IndicesAndWeightsParams(ArrayList<Integer> indicesParams, ArrayList<String> weightsParams) {
this.indicesParams = indicesParams;
this.weightsParams = weightsParams;
}
}
И используя этот код, у меня все работает:
ArrayList<Integer> indices = new ArrayList<Integer>();
ArrayList<String> weights = new ArrayList<String>();
indices.add(2);
indices.add(5);
weights.add("fifty");
weights.add("twenty");
IndicesAndWeightsParams iaw = new IndicesAndWeightsParams(indices, weights);
Gson gson = new Gson();
String string = gson.toJson(iaw);
System.out.println(string);
IndicesAndWeightsParams fromJson = gson.fromJson(string, IndicesAndWeightsParams.class);
System.out.println(fromJson.indicesParams);
System.out.println(fromJson.weightsParams);