Gson parse error,当服务器响应数据为空字符串(本意是int类型)时,报错Invalid double: ""

Gson parse error, when the server response data is include an empty string(intent to be a int type), the error is Invalid double: ""

服务器响应数据如

{
  ...
  "number":"",
  ...
}

所以当我使用

Gson gson = new Gson();
gson.fromJson(data, obj.class);

将出现错误,似乎字符串为空。 我用 google 搜索了它,自定义 GsonBuilder 似乎可以解决这个问题,但真的如此吗?

试试这个,代替 "Object" 使用您需要转换为的 class 的对象。

Type type= new TypeToken< Object>(){}.getType();

Object obj=new Gson().fromJson(json,type);

好吧,因为评论的提示,我改变了我的Google搜索词,并找到了这个问题enter link description here

这个link可以解决我的问题。

所以我写了一个原始类型的typeadapter,终于成功了。