一、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/zh-hk/n/150849.html
微信掃一掃
支付寶掃一掃