/** * Get an optional JSONObject associated with a key.<p> * * It returns null if there is no such key, or if its value is not a * JSONObject.<p> * * @param key a key string * @return a JSONObject which is the value */ public JSONObject optJSONObject(String key) { Object o = opt(key); return o instanceof JSONObject ? (JSONObject)o : null; }
/** * Get an optional JSONArray associated with a key.<p> * * It returns null if there is no such key, or if its value is not a * JSONArray.<p> * * @param key a key string * @return a JSONArray which is the value */ public JSONArray optJSONArray(String key) { Object o = opt(key); return o instanceof JSONArray ? (JSONArray)o : null; }
/** * Get an optional JSONArray associated with a key.<p> * * It returns null if there is no such key, or if its value is not a * JSONArray.<p> * * @param key a key string * @return a JSONArray which is the value */ public JSONArray optJSONArray(String key) { Object o = opt(key); return o instanceof JSONArray ? (JSONArray)o : null; }
/** * Get an optional JSONObject associated with a key.<p> * * It returns null if there is no such key, or if its value is not a * JSONObject.<p> * * @param key a key string * @return a JSONObject which is the value */ public JSONObject optJSONObject(String key) { Object o = opt(key); return o instanceof JSONObject ? (JSONObject)o : null; }
/** * Determine if the value associated with the key is null or if there is no value.<p> * * @param key a key string * @return true if there is no value associated with the key or if * the value is the JSONObject.NULL object */ public boolean isNull(String key) { return JSONObject.NULL.equals(opt(key)); }
/** * Get an optional string associated with a key. * It returns the defaultValue if there is no such key.<p> * * @param key a key string * @param defaultValue the default * @return a string which is the value */ public String optString(String key, String defaultValue) { Object o = opt(key); return o != null ? o.toString() : defaultValue; }
/** * Determine if the value associated with the key is null or if there is no value.<p> * * @param key a key string * @return true if there is no value associated with the key or if * the value is the JSONObject.NULL object */ public boolean isNull(String key) { return JSONObject.NULL.equals(opt(key)); }
/** * Get an optional string associated with a key. * It returns the defaultValue if there is no such key.<p> * * @param key a key string * @param defaultValue the default * @return a string which is the value */ public String optString(String key, String defaultValue) { Object o = opt(key); return o != null ? o.toString() : defaultValue; }
/** * Get an optional double associated with a key, or the * defaultValue if there is no such key or if its value is not a number.<p> * * If the value is a string, an attempt will be made to evaluate it as * a number.<p> * * @param key a key string * @param defaultValue the default * @return an object which is the value */ public double optDouble(String key, double defaultValue) { try { Object o = opt(key); return o instanceof Number ? ((Number)o).doubleValue() : new Double((String)o).doubleValue(); } catch (Exception e) { return defaultValue; } }
/** * Get an optional double associated with a key, or the * defaultValue if there is no such key or if its value is not a number.<p> * * If the value is a string, an attempt will be made to evaluate it as * a number.<p> * * @param key a key string * @param defaultValue the default * @return an object which is the value */ public double optDouble(String key, double defaultValue) { try { Object o = opt(key); return o instanceof Number ? ((Number)o).doubleValue() : new Double((String)o).doubleValue(); } catch (Exception e) { return defaultValue; } }
/** * Construct a JSONObject from a subset of another JSONObject.<p> * * An array of strings is used to identify the keys that should be copied. * Missing keys are ignored.<p> * * @param jo a JSONObject * @param names an array of strings * @exception JSONException if a value is a non-finite number */ public JSONObject(JSONObject jo, String[] names) throws JSONException { this(); for (int i = 0; i < names.length; i += 1) { putOpt(names[i], jo.opt(names[i])); } }
/** * Construct a JSONObject from a subset of another JSONObject.<p> * * An array of strings is used to identify the keys that should be copied. * Missing keys are ignored.<p> * * @param jo a JSONObject * @param names an array of strings * @exception JSONException if a value is a non-finite number */ public JSONObject(JSONObject jo, String[] names) throws JSONException { this(); for (int i = 0; i < names.length; i += 1) { putOpt(names[i], jo.opt(names[i])); } }
/** * Get the value object associated with a key.<p> * * @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 { Object o = opt(key); if (o == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } return o; }
/** * Get the value object associated with a key.<p> * * @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 { Object o = opt(key); if (o == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } return o; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject.<p> * * @param names a JSONArray containing a list of key strings. This * determines the sequence of the values in the result * @return a JSONArray of values * @throws JSONException if any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject.<p> * * @param names a JSONArray containing a list of key strings. This * determines the sequence of the values in the result * @return a JSONArray of values * @throws JSONException if any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if ((names == null) || (names.length() == 0)) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(opt(names.getString(i))); } return ja; }
/** * Append values to the array under a key.<p> * * If the key does not exist in the * JSONObject, then the key is put in the JSONObject with its value being a * JSONArray containing the value parameter. If the key was already * associated with a JSONArray, then the value parameter is appended to it.<p> * * @param key a key string * @param value an object to be accumulated under the key * @return this * @throws JSONException if the key is null or if the current value * associated with the key is not a JSONArray */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, new JSONArray().put(value)); } else if (o instanceof JSONArray) { put(key, ((JSONArray)o).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); } return this; }
/** * Append values to the array under a key.<p> * * If the key does not exist in the * JSONObject, then the key is put in the JSONObject with its value being a * JSONArray containing the value parameter. If the key was already * associated with a JSONArray, then the value parameter is appended to it.<p> * * @param key a key string * @param value an object to be accumulated under the key * @return this * @throws JSONException if the key is null or if the current value * associated with the key is not a JSONArray */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, new JSONArray().put(value)); } else if (o instanceof JSONArray) { put(key, ((JSONArray)o).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); } return this; }
/** * Accumulate values under a key.<p> * * It is similar to the put method except * that if there is already an object stored under the key then a * JSONArray is stored under the key to hold all of the accumulated values. * If there is already a JSONArray, then the new value is appended to it. * In contrast, the put method replaces the previous value.<p> * * @param key a key string * @param value an object to be accumulated under the key * @return this * @throws JSONException if the value is an invalid number or if the key is null */ public JSONObject accumulate(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, value instanceof JSONArray ? new JSONArray().put(value) : value); } else if (o instanceof JSONArray) { ((JSONArray)o).put(value); } else { put(key, new JSONArray().put(o).put(value)); } return this; }
/** * Accumulate values under a key.<p> * * It is similar to the put method except * that if there is already an object stored under the key then a * JSONArray is stored under the key to hold all of the accumulated values. * If there is already a JSONArray, then the new value is appended to it. * In contrast, the put method replaces the previous value.<p> * * @param key a key string * @param value an object to be accumulated under the key * @return this * @throws JSONException if the value is an invalid number or if the key is null */ public JSONObject accumulate(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, value instanceof JSONArray ? new JSONArray().put(value) : value); } else if (o instanceof JSONArray) { ((JSONArray)o).put(value); } else { put(key, new JSONArray().put(o).put(value)); } return this; }