예제 #1
0
 public static String getJSONFromMap(Map<String, String> map) {
   String result = null;
   if (map != null) {
     try {
       JSONObject jsonObject = new JSONObject();
       for (Entry<String, String> entry : map.entrySet()) {
         jsonObject.put(entry.getKey(), entry.getValue());
       }
       result = jsonObject.toString();
     } catch (JSONException e) {
       // log
     }
   }
   return result;
 }
예제 #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.myHashMap.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
 public static Map<String, String> getMapFromJSON(String jsonString) {
   Map<String, String> descriptions = new HashMap<String, String>();
   if (jsonString != null) {
     try {
       JSONObject jsonObject = new JSONObject(jsonString);
       @SuppressWarnings("unchecked")
       Iterator<String> keyIterator = jsonObject.keys();
       while (keyIterator.hasNext()) {
         String type = (String) keyIterator.next();
         descriptions.put(type, jsonObject.getString(type));
       }
     } catch (JSONException e) {
       // log
     }
   }
   return descriptions;
 }
예제 #4
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 sa An array of strings.
  * @throws JSONException If a value is a non-finite number.
  */
 public JSONObject(JSONObject jo, String[] sa) throws JSONException {
   this();
   for (int i = 0; i < sa.length; i += 1) {
     putOpt(sa[i], jo.opt(sa[i]));
   }
 }