Exemple #1
0
 /**
  * Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
  *
  * @param names A JSONArray containing a list of key strings. These will be paired with the
  *     values.
  * @return A JSONObject, or null if there are no names or if this JSONArray has no values.
  * @throws JSONException If any of the names are null.
  */
 public JSONObject toJSONObject(JSONArray names) throws JSONException {
   if (names == null || names.length() == 0 || length() == 0) {
     return null;
   }
   JSONObject jo = new JSONObject();
   for (int i = 0; i < names.length(); i += 1) {
     jo.put(names.getString(i), this.opt(i));
   }
   return jo;
 }
Exemple #2
0
  /**
   * Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is
   * added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean b = false;
      int len = length();

      writer.write('[');

      for (int i = 0; i < len; i += 1) {
        if (b) {
          writer.write(',');
        }
        Object v = this.myArrayList.get(i);
        if (v instanceof JSONObject) {
          ((JSONObject) v).write(writer);
        } else if (v instanceof JSONArray) {
          ((JSONArray) v).write(writer);
        } else {
          writer.write(JSONObject.valueToString(v));
        }
        b = true;
      }
      writer.write(']');
      return writer;
    } catch (IOException e) {
      throw new JSONException(e);
    }
  }