code: public JSONObject accumulate(String key, Object value) throws JSONException {
          testValidity(value);
          Object object = this.opt(key);
          if (object == null) {
              this.put(key,
                      value instanceof JSONArray ? new JSONArray().put(value)
                              : value);
          } else if (object instanceof JSONArray) {
              ((JSONArray) object).put(value);
          } else {
              this.put(key, new JSONArray().put(object).put(value));
          }
          return this;
      }
docs: 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.
  
  If only one value is accumulated that is not a JSONArray, then the result
  will be the same as using put. But if multiple values are accumulated,
  then the result will be like append.
  
  @param key
  A key string.
  @param value
  An object to be accumulated under the key.
  @return this.
  @throws JSONException
  If the value is non-finite number.
  @throws NullPointerException
  If the key is <code>null</code>.