引起:java.lang.NumberFormatException:无效的double:“”带有空值

时间:2016-07-29 10:38:11

标签: java json number-formatting

JSON字符串有一些空白值,从服务器获取时,我得到了错误 如何处理空白值  这是JSON

{  
   "status":"success",
   "data":[  
      {  
         "id":"1",
         "name":"ABC",
         "address":"ABC, QWE",
         "lat":"16.799999",
         "lng":"96.150002",
         "admin_id":"4",
         "is_approved":"1",
         "added":"2015-08-07 11:17:12",
         "status":"1",
         "image_file":"",
         "image_width":"",
         "image_height":""
      }
   ]
}

Gson gson = new GsonBuilder().serializeNulls().create();
Type listType = new TypeToken<List<MyData>>() {}.getType();
myDataArrayList = gson.fromJson(response.getString("data"), listType);

在MyData类中,我尝试更改lat, long类型int并更改json中的值同样但仍然显示相同的错误

public class MyData implements Parcelable {

    public int id;

    public String name;

    public String address;

    public String lat;

    public String lng;

    public String added;

    public int status;

    public String image_file;

    public int image_width;

    public int image_height;

}

在此行中显示错误

myDataArrayList = gson.fromJson(response.getString("data"), listType);

2 个答案:

答案 0 :(得分:1)

在MyData Class中将image_width和image_height的类型更改为String

public String image_width;

public String image_height;

因为int将无法存储“”此字符串,并且在使用它时您可以将其解析为int。

答案 1 :(得分:0)

public int image_width;     // Expected integer value while parsing 

public int image_height;   // Expected integer value while parsing

如下所示,您有字符串字段。将下面的值初始化为0

"image_width":0,
"image_height":0