public static String quote(String string) { return JSONObject.quote(string); }
/** * Produce a string in double quotes with backslash sequences in all the * right places. A backslash will be inserted within </, producing <\/, * allowing JSON text to be delivered in HTML. In JSON text, a string cannot * contain a control character or an unescaped quote or backslash. * * @param string * A String * @return A String correctly formatted for insertion in a JSON text. */ public static String quote(String string) { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { try { return quote(string, sw).toString(); } catch (IOException ignored) { // will never happen - we are writing to a string writer return ""; } } }
/** * Get the JSONArray value associated with a key. * * @param key * A key string. * @return A JSONArray which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONArray. */ public JSONArray getJSONArray(String key) throws JSONException { Object object = this.get(key); if (object instanceof JSONArray) { return (JSONArray) object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Get the string associated with a key. * * @param key * A key string. * @return A string which is the value. * @throws JSONException * if there is no string value for the key. */ public String getString(String key) throws JSONException { Object object = this.get(key); if (object instanceof String) { return (String) object; } throw new JSONException("JSONObject[" + quote(key) + "] not a string."); }
/** * Get the JSONObject value associated with a key. * * @param key * A key string. * @return A JSONObject which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONObject. */ public JSONObject getJSONObject(String key) throws JSONException { Object object = this.get(key); if (object instanceof JSONObject) { return (JSONObject) object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the int value associated with a key. * * @param key * A key string. * @return The integer value. * @throws JSONException * if the key is not found or if the value cannot be converted * to an integer. */ public int getInt(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).intValue() : Integer.parseInt((String) object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not an int."); } }
/** * Get the double value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public double getDouble(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).doubleValue() : Double.parseDouble((String) object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); } }
/** * Get the long value associated with a key. * * @param key * A key string. * @return The long value. * @throws JSONException * if the key is not found or if the value cannot be converted * to a long. */ public long getLong(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).longValue() : Long.parseLong((String) object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a long."); } }
/** * Get the string associated with a key. * * @param key * A key string. * @return A string which is the value. * @throws JSONException * if there is no string value for the key. */ public String getString(String key) throws JSONException { Object object = this.get(key); if (object instanceof String) { return (String) object; } throw new JSONException("JSONObject[" + quote(key) + "] not a string."); }
/** * Get the boolean value associated with a key. * * @param key * A key string. * @return The truth. * @throws JSONException * if the value is not a Boolean or the String "true" or * "false". */ public boolean getBoolean(String key) throws JSONException { Object object = this.get(key); if (object.equals(Boolean.FALSE) || (object instanceof String && ((String) object) .equalsIgnoreCase("false"))) { return false; } else if (object.equals(Boolean.TRUE) || (object instanceof String && ((String) object) .equalsIgnoreCase("true"))) { return true; } throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean."); }
/** * Get the JSONArray value associated with a key. * * @param key * A key string. * @return A JSONArray which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONArray. */ public JSONArray getJSONArray(String key) throws JSONException { Object object = this.get(key); if (object instanceof JSONArray) { return (JSONArray) object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Get the JSONObject value associated with a key. * * @param key * A key string. * @return A JSONObject which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONObject. */ public JSONObject getJSONObject(String key) throws JSONException { Object object = this.get(key); if (object instanceof JSONObject) { return (JSONObject) object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the BigInteger value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value cannot * be converted to BigInteger. */ public BigInteger getBigInteger(String key) throws JSONException { Object object = this.get(key); try { return new BigInteger(object.toString()); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] could not be converted to BigInteger.", e); } }
/** * Get the double value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public double getDouble(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).doubleValue() : Double.parseDouble(object.toString()); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number.", e); } }
/** * Get the float value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public float getFloat(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).floatValue() : Float.parseFloat(object.toString()); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number.", e); } }
/** * Get the int value associated with a key. * * @param key * A key string. * @return The integer value. * @throws JSONException * if the key is not found or if the value cannot be converted * to an integer. */ public int getInt(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).intValue() : Integer.parseInt((String) object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not an int.", e); } }
/** * Get the long value associated with a key. * * @param key * A key string. * @return The long value. * @throws JSONException * if the key is not found or if the value cannot be converted * to a long. */ public long getLong(String key) throws JSONException { Object object = this.get(key); try { return object instanceof Number ? ((Number) object).longValue() : Long.parseLong((String) object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a long.", e); } }
/** * Increment a property of a JSONObject. If there is no such property, * create one with a value of 1. If there is such a property, and if it is * an Integer, Long, Double, or Float, then add one to it. * * @param key * A key string. * @return this. * @throws JSONException * If there is already a property with this name that is not an * Integer, Long, Double, or Float. */ public JSONObject increment(String key) throws JSONException { Object value = this.opt(key); if (value == null) { this.put(key, 1); } else if (value instanceof Integer) { this.put(key, (Integer) value + 1); } else if (value instanceof Long) { this.put(key, (Long) value + 1); } else if (value instanceof Double) { this.put(key, (Double) value + 1); } else if (value instanceof Float) { this.put(key, (Float) value + 1); } else { throw new JSONException("Unable to increment [" + quote(key) + "]."); } return this; }
/** * Get the value object associated with a key. * * @param key * A key string. * @return The object associated with the key. * @throws JSONException * if the key is not found. */ public Object get(String key) throws JSONException { if (key == null) { throw new JSONException("Null key."); } Object object = this.opt(key); if (object == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } return object; }
/** * Get the value object associated with a key. * * @param key * A key string. * @return The object associated with the key. * @throws JSONException * if the key is not found. */ public Object get(String key) throws JSONException { if (key == null) { throw new JSONException("Null key."); } Object object = this.opt(key); if (object == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } return object; }