Skip to content

Instantly share code, notes, and snippets.

@KazaKago
Last active September 11, 2015 02:32
Show Gist options
  • Save KazaKago/91fa94d86b91d26c6c06 to your computer and use it in GitHub Desktop.
Save KazaKago/91fa94d86b91d26c6c06 to your computer and use it in GitHub Desktop.
Nullを考慮したJSONObject操作用ユーティリティクラス
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* JSONObjectを扱うUtilクラス<br>
* 基本型もオブジェクト型として返却するため項目がnullの場合にはnullを返す
*
* @author KCMEUser
*/
public class JSONObjectUtil {
public static Object get(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.get(name) : null;
}
public static Object get(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.get(i) : null;
}
public static Integer getInteger(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getInt(name) : null;
}
public static Integer getInteger(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getInt(i) : null;
}
public static Long getLong(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getLong(name) : null;
}
public static Long getLong(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getLong(i) : null;
}
public static Double getDouble(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getDouble(name) : null;
}
public static Double getDouble(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getDouble(i) : null;
}
public static Boolean getBoolean(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getBoolean(name) : null;
}
public static Boolean getBoolean(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getBoolean(i) : null;
}
public static String getString(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getString(name) : null;
}
public static String getString(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getString(i) : null;
}
public static JSONObject getJSONObject(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getJSONObject(name) : null;
}
public static JSONObject getJSONObject(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getJSONObject(i) : null;
}
public static JSONArray getJSONArray(JSONObject jsonObject, String name) throws JSONException {
return (jsonObject.has(name) && !jsonObject.isNull(name)) ? jsonObject.getJSONArray(name) : null;
}
public static JSONArray getJSONArray(JSONArray jsonArray, int i) throws JSONException {
return (i < jsonArray.length() && !jsonArray.isNull(i)) ? jsonArray.getJSONArray(i) : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment