由于Java的类型擦除,Gson在集合方面有一些限制。您可以在此处了解更多信息。
从您的问题中,我看到您同时使用
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);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)