/** * Returns the value mapped by {@code name} if it exists and is an int or can be coerced to an * int. * * @throws JSONException if the mapping doesn't exist or cannot be coerced to an int. */ public int getInt(String name) throws JSONException { Object object = get(name); Integer result = JSON.toInteger(object); if (result == null) { throw JSON.typeMismatch(name, object, "int"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is an int or can be coerced to an * int. Returns {@code fallback} otherwise. */ public int optInt(String name, int fallback) { Object object = opt(name); Integer result = JSON.toInteger(object); return result != null ? result : fallback; }