class JsonHelper {

    public <T> T getKeyValue(JSONObject jsonObject, Object key, Class<T> classType)
    {
        Object result = jsonObject.get(key);

        if (result == null)
        {
            throw new IllegalArgumentException(String.format("Failed to get \"%s\" key value from JSON object.", key.toString()));
        }

        try
        {
            return classType.cast(result);
        }
        catch (ClassCastException e)
        {
            throw new IllegalArgumentException(String.format("Failed to cast \"%s\" key value to %s.", result.toString(), classType.getName()), e);
        }
    }

}