Example #1
0
 private static Trend[] jsonArrayToTrendArray(JSONArray array) throws JSONException {
   Trend[] trends = new Trend[array.length()];
   for (int i = 0; i < array.length(); i++) {
     JSONObject trend = array.getJSONObject(i);
     trends[i] = new Trend(trend);
   }
   return trends;
 }
 /**
  * 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;
 }
Example #3
0
 /* modify by hezhou add some field*/
 static List<Comment> constructComments(Response res) throws WeiboException {
   try {
     JSONArray list = res.asJSONArray();
     int size = list.length();
     List<Comment> comments = new ArrayList<Comment>(size);
     for (int i = 0; i < size; i++) {
       comments.add(new Comment(list.getJSONObject(i)));
     }
     return comments;
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   } catch (WeiboException te) {
     throw te;
   }
 }
  /**
   * 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);
    }
  }