JSON数据中存在空值的问题

由于网站上的JSON数据是实时更新的,就会有一些字段的值为空,所以在解析时需要先对其进行判断,不然会出现空指针异常等。

处理image字段的值的方法:

Object obj = itemObject.get("image");  //itemObject为已获取的JSONObject对象

if(obj.toString!="null"){

  String image = (String)obj;  //向下转型

  item.setImage(image);  //item为类Item的实例,setImage()为image的setter方法。

}

posted @ 2015-04-23 15:58  QuestG  阅读(1877)  评论(0)    收藏  举报