Exemplo n.º 1
0
 /**
  * Construct a JSONArray from a JSONTokener.
  *
  * @param x A JSONTokener
  * @throws JSONException If there is a syntax error.
  */
 public JSONArray(JSONTokener x) throws JSONException {
   this();
   if (x.nextClean() != '[') {
     throw x.syntaxError("A JSONArray text must start with '['");
   }
   if (x.nextClean() != ']') {
     x.back();
     for (; ; ) {
       if (x.nextClean() == ',') {
         x.back();
         this.myArrayList.add(JSONObject.NULL);
       } else {
         x.back();
         this.myArrayList.add(x.nextValue());
       }
       switch (x.nextClean()) {
         case ';':
         case ',':
           if (x.nextClean() == ']') {
             return;
           }
           x.back();
           break;
         case ']':
           return;
         default:
           throw x.syntaxError("Expected a ',' or ']'");
       }
     }
   }
 }
 /**
  * Determine if the source string still contains characters that next() can consume.
  *
  * @return true if not yet at the end of the source.
  */
 public boolean more() throws JSONException {
   next();
   if (end()) {
     return false;
   }
   back();
   return true;
 }
  /**
   * Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long,
   * or String, or the JSONObject.NULL object.
   *
   * @return An object.
   * @throws JSONException If syntax error.
   */
  public Object nextValue() throws JSONException {
    char c = nextClean();
    String string;

    switch (c) {
      case '"':
      case '\'':
        return nextString(c);
      case '{':
        back();
        return new JSONObject(this);
      case '[':
        back();
        return new JSONArray(this);
    }

    /*
     * Handle unquoted text. This could be the values true, false, or
     * null, or it can be a number. An implementation (such as this one)
     * is allowed to also accept non-standard forms.
     *
     * Accumulate characters until we reach the end of the text or a
     * formatting character.
     */

    StringBuilder sb = new StringBuilder();
    while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
      sb.append(c);
      c = next();
    }
    back();

    string = sb.toString().trim();
    if (string.equals("")) {
      throw syntaxError("Missing value");
    }
    return JSONObject.stringToValue(string);
  }
Exemplo n.º 4
0
 public JSONArray(JSONTokener paramJSONTokener) throws JSONException {
   this();
   if (paramJSONTokener.nextClean() != '[')
     throw paramJSONTokener.syntaxError("A JSONArray text must start with '['");
   if (paramJSONTokener.nextClean() != ']') paramJSONTokener.back();
   while (true) {
     if (paramJSONTokener.nextClean() == ',') {
       paramJSONTokener.back();
       this.myArrayList.add(JSONObject.NULL);
     }
     while (true)
       switch (paramJSONTokener.nextClean()) {
         default:
           throw paramJSONTokener.syntaxError("Expected a ',' or ']'");
           paramJSONTokener.back();
           this.myArrayList.add(paramJSONTokener.nextValue());
         case ',':
         case ';':
         case ']':
       }
     if (paramJSONTokener.nextClean() == ']') return;
     paramJSONTokener.back();
   }
 }