コード例 #1
0
        /**
         * Construct a JSONArray from a JSONTokener.
         * @param x A JSONTokener
         * @throws JSONException If there is a syntax error.
         */
        public JSONArray(JSONTokener x)
            : this()
        {
            if (x.nextClean() != '[')
            {
                throw x.syntaxError("A JSONArray text must start with '['");
            }
            if (x.nextClean() == ']')
            {
                return;
            }
            x.back();
            for (; ;)
            {
                if (x.nextClean() == ',')
                {
                    x.back();
                    this.myArrayList.Add(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 ']'");
                }
            }
        }
コード例 #2
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.
         */
        public JSONObject(JSONTokener x)
        {
            this.myHashMap = new Hashtable();
            char   c;
            String key;

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

                case '}':
                    return;

                default:
                    x.back();
                    key = x.nextValue().ToString();
                    break;
                }

                /*
                 * 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");
                }
                put(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 '}'");
                }
            }
        }