示例#1
0
 /**
  * Convert a cookie list into a JSONObject. A cookie list is a sequence of name/value pairs. The
  * names are separated from the values by '='. The pairs are separated by ';'. The names and the
  * values will be unescaped, possibly converting '+' and '%' sequences. To add a cookie to a
  * cooklist, cookielistJSONObject.put(cookieJSONObject.getString("name"),
  * cookieJSONObject.getString("value"));
  *
  * @param string A cookie list string
  * @return A JSONObject
  * @throws JSONException If something goes wrong
  */
 public static JSONObject toJSONObject(final String string) throws JSONException {
   final JSONObject jo = new JSONObject();
   final JSONTokener x = new JSONTokener(string);
   while (x.more()) {
     final String name = Cookie.unescape(x.nextTo('='));
     x.next('=');
     jo.put(name, Cookie.unescape(x.nextTo(';')));
     x.next();
   }
   return jo;
 }
示例#2
0
 /**
  * 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;
 }
示例#3
0
  /**
   * Construct a JSONObject from a JSONTokener.
   *
   * @param x A JSONTokener object containing the source string.
   * @throws JSONException If there is a syntax error in the source string or a duplicated key.
   */
  public JSONObject(JSONTokener x) throws JSONException {
    this();
    char c;
    String key;

    if (x.nextClean() != '{') {
      throw x.syntaxError("A JSONObject text must begin with '{'");
    }
    for (; ; ) {
      c = x.nextClean();
      switch (c) {
        case 0:
          throw x.syntaxError("A JSONObject text must end with '}'");
        case '}':
          return;
        default:
          x.back();
          key = x.nextValue().toString();
      }

      /*
       * The key is followed by ':'. We will also tolerate '=' or '=>'.
       */

      c = x.nextClean();
      if (c == '=') {
        if (x.next() != '>') {
          x.back();
        }
      } else if (c != ':') {
        throw x.syntaxError("Expected a ':' after a key");
      }
      putOnce(key, x.nextValue());

      /*
       * Pairs are separated by ','. We will also tolerate ';'.
       */

      switch (x.nextClean()) {
        case ';':
        case ',':
          if (x.nextClean() == '}') {
            return;
          }
          x.back();
          break;
        case '}':
          return;
        default:
          throw x.syntaxError("Expected a ',' or '}'");
      }
    }
  }