Expand Minimize Picture-in-picture Power Device Status Voice Recognition Skip Back Skip Forward Minus Plus Play Search
Documentation
Sidebar

Class JSONTokener

Hierarchy

java.lang.Object

Package
org.json

Overview

Parses a JSON (RFC 4627) encoded string into the corresponding object. Most clients of this class will use only need the {@link #JSONTokener(String) constructor} and {@link #nextValue} method. Example usage:
 String json = "{"
         + "  \"query\": \"Pizza\", "
         + "  \"locations\": [ 94043, 90210 ] "
         + "}";

 JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
 String query = object.getString("query");
 JSONArray locations = object.getJSONArray("locations");

For best interoperability and performance use JSON that complies with RFC 4627, such as that generated by JSONStringer. For legacy reasons this parser is lenient, so a successful parse does not indicate that the input string was valid JSON. All of the following syntax errors will be ignored:

  • End of line comments starting with {@code //} or {@code #} and ending with a newline character.
  • C-style comments starting with {@code /*} and ending with {@code *}{@code /}. Such comments may not be nested.
  • Strings that are unquoted or {@code 'single quoted'}.
  • Hexadecimal integers prefixed with {@code 0x} or {@code 0X}.
  • Octal integers prefixed with {@code 0}.
  • Array elements separated by {@code ;}.
  • Unnecessary array separators. These are interpreted as if null was the omitted value.
  • Key-value pairs separated by {@code =} or {@code =>}.
  • Key-value pairs separated by {@code ;}.

Each tokener may be used to parse a single JSON string. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

Constructors

JSONTokener( String in )

public JSONTokener(String in)

Parameters

in
JSON encoded string. Null is not permitted and will yield a tokener that throws {@code NullPointerExceptions} when methods are called.

Methods

nextValue()

Returns the next value from the input.

public java.lang.Object nextValue() throws org.json.JSONException

Throws

nextString( char quote )

Returns the string up to but not including {@code quote}, unescaping any character escape sequences encountered along the way. The opening quote should have already been read. This consumes the closing quote, but does not include it in the returned string.

public java.lang.String nextString(char quote) throws org.json.JSONException

Parameters

quote
either ' or ".

Throws

syntaxError( String message )

Returns an exception containing the given message plus the current position and the entire input string.

public org.json.JSONException syntaxError(String message)

Parameters

message

toString()

Returns the current position and the entire input string.

public java.lang.String toString()

more()

Returns true until the input has been exhausted.

public boolean more()

next()

Returns the next available character, or the null character '\0' if all input has been exhausted. The return value of this method is ambiguous for JSON strings that contain the character '\0'.

public char next()

next( char c )

Returns the next available character if it equals {@code c}. Otherwise an exception is thrown.

public char next(char c) throws org.json.JSONException

Parameters

c

Throws

nextClean()

Returns the next character that is not whitespace and does not belong to a comment. If the input is exhausted before such a character can be found, the null character '\0' is returned. The return value of this method is ambiguous for JSON strings that contain the character '\0'.

public char nextClean() throws org.json.JSONException

Throws

next( int length )

Returns the next {@code length} characters of the input.

The returned string shares its backing character array with this tokener's input string. If a reference to the returned string may be held indefinitely, you should use {@code new String(result)} to copy it first to avoid memory leaks.

public java.lang.String next(int length) throws org.json.JSONException

Parameters

length

Throws

nextTo( String excluded )

Returns the {@link String#trim trimmed} string holding the characters up to but not including the first of:

  • any character in {@code excluded}
  • a newline character '\n'
  • a carriage return '\r'

The returned string shares its backing character array with this tokener's input string. If a reference to the returned string may be held indefinitely, you should use {@code new String(result)} to copy it first to avoid memory leaks.

public java.lang.String nextTo(String excluded)

Parameters

excluded

nextTo( char excluded )

Equivalent to {@code nextTo(String.valueOf(excluded))}.

public java.lang.String nextTo(char excluded)

Parameters

excluded

skipPast( String thru )

Advances past all input up to and including the next occurrence of {@code thru}. If the remaining input doesn't contain {@code thru}, the input is exhausted.

public void skipPast(String thru)

Parameters

thru

skipTo( char to )

Advances past all input up to but not including the next occurrence of {@code to}. If the remaining input doesn't contain {@code to}, the input is unchanged.

public char skipTo(char to)

Parameters

to

back()

Unreads the most recent character of input. If no input characters have been read, the input is unchanged.

public void back()

dehexchar( char hex )

Returns the integer [0..15] value for the given hex character, or -1 for non-hex input.

public static int dehexchar(char hex)

Parameters

hex
a character in the ranges [0-9], [A-F] or [a-f]. Any other character will yield a -1 result.

Inherited Methods

From Class Methods
java.lang.Object getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize
View on GitHub.com
Previous Section Next Section