示例#1
0
 /**
  * Get an array of field names from a JSONObject.
  *
  * @return An array of field names, or null if there are no names.
  */
 public static String[] getNames(JSONObject jo) {
   int length = jo.length();
   if (length == 0) {
     return null;
   }
   Iterator i = jo.keys();
   String[] names = new String[length];
   int j = 0;
   while (i.hasNext()) {
     names[j] = (String) i.next();
     j += 1;
   }
   return names;
 }
示例#2
0
  /**
   * Write the contents of the JSONObject 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;
      Iterator keys = keys();
      writer.write('{');

      while (keys.hasNext()) {
        if (b) {
          writer.write(',');
        }
        Object k = keys.next();
        writer.write(quote(k.toString()));
        writer.write(':');
        Object v = this.map.get(k);
        if (v instanceof JSONObject) {
          ((JSONObject) v).write(writer);
        } else if (v instanceof JSONArray) {
          ((JSONArray) v).write(writer);
        } else {
          writer.write(valueToString(v));
        }
        b = true;
      }
      writer.write('}');
      return writer;
    } catch (IOException e) {
      throw new JSONException(e);
    }
  }
示例#3
0
 /**
  * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to
  * identify the keys that should be copied. Missing keys are ignored.
  *
  * @param jo A JSONObject.
  * @param names An array of strings.
  * @exception JSONException If a value is a non-finite number or if a name is duplicated.
  */
 public JSONObject(JSONObject jo, String[] names) throws JSONException {
   this();
   for (int i = 0; i < names.length; i += 1) {
     putOnce(names[i], jo.opt(names[i]));
   }
 }