使用 Gson 将 JSON 反序列化为对象时出现以下异常:
com.google.gson.JsonParseException: JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@588722d6 未能反序列化 json 对象 [{"time":1378911600000,"total":0},{"time":1378912500000,"total ":0},{"time":1378913400000,"total":2,"sum":130000,"avgLen":65000.0}] 给定类型 com.google.gson.ParameterizedTypeImpl@490ca2fa
应该代表 JSON 的类是:
public class Api{
private List<ApiData> avgEngLength;
public Api() {
}
}
public class ApiData{
private Long time;
private Long total;
private Long sum;
private Double avgLen;
public ApiData(Long time, Long total, Long sum, Double avgLen) {
this.time = time;
this.total= total;
this.sum= sum;
this.avgLen= avgLen;
}
}
反序列化的代码是:
String json = "{\"avgEngLength\":[{\"time\":1378905300000,\"total\":0},{\"time\":1378906200000,\"total\":2,\"sum\":130000,\"avgLen\":65000.0}]}";
Gson gson = new GsonBuilder().create();
return gson.fromJson(json, Api.class);
奇怪的是,它适用于某些机器而不适用于其他机器。任何想法?