JSON中getString与optString方法的区别

JSONObject json = new JSONObject();
json.put("cmd","1");
json.put("user_id","0001");

//当JSONObject中包含Key值时,效果相同
json.getString("cmd");
json.optString("cmd");

//当JSONObject中Key值对应的Value为null时,效果不同
json.getString("room_id");//抛出异常
json.optString("room_id");//返回空字符串

猜你喜欢

转载自blog.csdn.net/qq_27124771/article/details/81198617