[英]Failed to deserialize an object with List using Gson
使用Gson將JSON反序列化為對象時,出現以下異常:
com.google.gson.JsonParseException:JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@588722d6無法反序列化json對象[{“ time”:1378911600000,“ total”:0},{“ time”:1378912500000,“ total給出類型com.google.gson.ParameterizedTypeImpl@490ca2fa的“:0},{” time“:1378913400000,” total“:2,” sum“:130000,” avgLen“:65000.0}]
應該表示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);
奇怪的是,它只能在某些機器上運行,而不能在其他機器上運行。 任何想法?
我用這個嘗試了您的示例:
public static void main(String[] args) {
String s = "{\"avgEngLength\":[{\"time\":1378905300000,\"total\":0},{\"time\":1378906200000,\"total\":2,\"sum\":130000,\"avgLen\":65000.0}]}";
Gson gson = new GsonBuilder().create();
Api a = gson.fromJson(s, Api.class);
System.out.println(a);
}
並且有效(請注意,示例中的字符串沒有轉義引號)。
Api [avgEngLength=[ApiData [time=1378905300000, total=0, sum=null, avgLen=null], ApiData [time=1378906200000, total=2, sum=130000, avgLen=65000.0]]]
因此,最好的猜測是您的團隊正在使用該庫的不同版本。 我正在使用Gson 2.2.4,並檢查了源代碼:該錯誤字符串在庫中不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.