Exemple #1
0
 /**
  * Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from
  * the JSONObject if it is present.
  *
  * @param key A key string.
  * @param value An object which is the value. It should be of one of these types: Boolean, Double,
  *     Integer, JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.
  * @return this.
  * @throws JSONException If the value is non-finite number or if the key is null.
  */
 public JSONObject put(String key, Object value) throws JSONException {
   if (key == null) {
     throw new JSONException("Null key.");
   }
   if (value != null) {
     testValidity(value);
     this.map.put(key, value);
   } else {
     remove(key);
   }
   return this;
 }
Exemple #2
0
 /**
  * Append values to the array under a key. 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.
  *
  * @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;
 }
Exemple #3
0
 /**
  * Accumulate values under a key. 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.
  *
  * @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;
 }
Exemple #4
0
  /**
   * Produce a string from a Number.
   *
   * @param n A Number
   * @return A String.
   * @throws JSONException If n is a non-finite number.
   */
  public static String numberToString(Number n) throws JSONException {
    if (n == null) {
      throw new JSONException("Null pointer");
    }
    testValidity(n);

    // Shave off trailing zeros and decimal point, if possible.

    String s = n.toString();
    if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
      while (s.endsWith("0")) {
        s = s.substring(0, s.length() - 1);
      }
      if (s.endsWith(".")) {
        s = s.substring(0, s.length() - 1);
      }
    }
    return s;
  }