一、has方法
/**
* 判断JSONObject中是否存在指定的Key
* @param jsonObject JSONObject对象
* @param key Key
* @return 存在返回true,否则返回false
*/
public static boolean has(JSONObject jsonObject, String key) {
return jsonObject != null && jsonObject.has(key);
}
在Java中,使用JSONObject来解析JSON字符串是一个很常见的操作,我们通常会遇到需要判断JSONObject中是否存在指定的Key的需求。而使用has方法可以很方便地实现这个功能。has方法是JSONObject类自带的方法,它用于判断JSONObject中是否存在指定的Key,如果存在返回true,否则返回false。
例如:
JSONObject jsonObject = new JSONObject("{\"name\":\"张三\",\"age\":20}");
boolean hasName = has(jsonObject, "name");
boolean hasGender = has(jsonObject, "gender");
System.out.println("hasName:" + hasName); // 输出 true
System.out.println("hasGender:" + hasGender); // 输出 false
二、isNull方法
/**
* 判断JSONObject中指定的Key是否为null或者不存在
* @param jsonObject JSONObject对象
* @param key Key
* @return 为null或者不存在返回true,否则返回false
*/
public static boolean isNull(JSONObject jsonObject, String key) {
return jsonObject == null || jsonObject.isNull(key);
}
有时候我们不仅需要判断JSONObject中是否存在指定的Key,还需要判断这个Key的值是否为null。此时我们可以使用JSONObject的isNull方法。isNull方法用于判断指定的Key在JSONObject中是否为null或者不存在,如果是返回true,否则返回false。
例如:
JSONObject jsonObject = new JSONObject("{\"name\":null,\"age\":20}");
boolean isNameNull = isNull(jsonObject, "name");
boolean isGenderNull = isNull(jsonObject, "gender");
System.out.println("isNameNull:" + isNameNull); // 输出 true
System.out.println("isGenderNull:" + isGenderNull); // 输出 true
三、opt方法
/**
* 获取JSONObject中指定Key对应的值,如果不存在则返回null
* @param jsonObject JSONObject对象
* @param key Key
* @return 如果存在返回对应的值,否则返回null
*/
public static Object opt(JSONObject jsonObject, String key) {
return jsonObject == null ? null : jsonObject.opt(key);
}
除了使用has方法和isNull方法来判断JSONObject中是否存在指定的Key外,我们还可以使用opt方法来获得指定Key的值。opt方法也是JSONObject类自带的方法,用于获取JSONObject中指定的Key对应的值,如果不存在则返回null。
例如:
JSONObject jsonObject = new JSONObject("{\"name\":\"张三\",\"age\":20}");
String name = (String) opt(jsonObject, "name");
String gender = (String) opt(jsonObject, "gender");
System.out.println("name:" + name); // 输出 张三
System.out.println("gender:" + gender); // 输出 null
四、get方法
/**
* 获取JSONObject中指定Key对应的值,如果不存在则抛出异常
* @param jsonObject JSONObject对象
* @param key Key
* @return 如果存在返回对应的值
* @throws JSONException 如果Key不存在抛出异常
*/
public static Object get(JSONObject jsonObject, String key) throws JSONException {
if (jsonObject == null) {
return null;
}
if (jsonObject.has(key)) {
return jsonObject.get(key);
} else {
throw new JSONException("JSONObject中不存在\"" + key + "\"");
}
}
get方法和opt方法类似,都是用于获取JSONObject中指定的Key对应的值。区别在于,如果指定Key不存在,opt方法会返回null,而get方法会抛出JSONException异常。
例如:
try {
JSONObject jsonObject = new JSONObject("{\"name\":\"张三\",\"age\":20}");
String name = (String) get(jsonObject, "name");
String gender = (String) get(jsonObject, "gender");
System.out.println("name:" + name); // 输出 张三
System.out.println("gender:" + gender); // 抛出JSONException异常,因为gender不存在
} catch (JSONException e) {
e.printStackTrace();
}
五、判断JSONObject是否为空
有时候我们需要判断JSONObject是否为空,即是否没有任何Key。我们可以使用JSONObject的length方法来判断JSONObject中有多少个Key,如果为0则表示JSONObject为空。
/**
* 判断JSONObject是否为空
* @param jsonObject JSONObject对象
* @return 为空返回true,否则返回false
*/
public static boolean isEmpty(JSONObject jsonObject) {
return jsonObject == null || jsonObject.length() == 0;
}
例如:
JSONObject jsonObject = new JSONObject("{}");
boolean isEmpty = isEmpty(jsonObject);
System.out.println("isEmpty:" + isEmpty); // 输出 true
结语
在开发过程中,判断JSONObject中是否存在指定的Key是一项非常基础的操作,我们可以使用has、isNull、opt、get等方法来实现这个功能。另外,在使用JSONObject时,我们也需要时刻注意异常处理,避免出现空指针异常等问题。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/150849.html
微信扫一扫
支付宝扫一扫