Added in API level 1

JSONTokener

open class JSONTokener
kotlin.Any
   ↳ org.json.JSONTokener

Parses a JSON (RFC 4627) encoded string into the corresponding object. Most clients of this class will use only need the constructor and 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 // or and ending with a newline character.
  • C-style comments starting with /* and ending with */. Such comments may not be nested.
  • Strings that are unquoted or 'single quoted'.
  • Hexadecimal integers prefixed with 0x or 0X.
  • Octal integers prefixed with 0.
  • Array elements separated by ;.
  • Unnecessary array separators. These are interpreted as if null was the omitted value.
  • Key-value pairs separated by = or =>.
  • Key-value pairs separated by ;.

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.

Summary

Public constructors

Public methods
open Unit

Unreads the most recent character of input.

open static Int

Returns the integer [0.

open Boolean

Returns true until the input has been exhausted.

open Char

Returns the next available character, or the null character '\0' if all input has been exhausted.

open Char
next(c: Char)

Returns the next available character if it equals c.

open String!
next(length: Int)

Returns the next length characters of the input.

open Char

Returns the next character that is not whitespace and does not belong to a comment.

open String!
nextString(quote: Char)

Returns the string up to but not including quote, unescaping any character escape sequences encountered along the way.

open String!
nextTo(excluded: String!)

Returns the trimmed string holding the characters up to but not including the first of:

  • any character in excluded
  • a newline character '\n'
  • a carriage return '\r'

open String!
nextTo(excluded: Char)

Equivalent to nextTo(String.valueOf(excluded)).

open Any!

Returns the next value from the input.

open Unit
skipPast(thru: String!)

Advances past all input up to and including the next occurrence of thru.

open Char
skipTo(to: Char)

Advances past all input up to but not including the next occurrence of to.

open JSONException!
syntaxError(message: String!)

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

open String

Returns the current position and the entire input string.

Public constructors

JSONTokener

Added in API level 1
JSONTokener(in: String!)
Parameters
in String!: JSON encoded string. Null is not permitted and will yield a tokener that throws NullPointerExceptions when methods are called.

Public methods

back

Added in API level 1
open fun back(): Unit

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

dehexchar

Added in API level 1
open static fun dehexchar(hex: Char): Int

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

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

more

Added in API level 1
open fun more(): Boolean

Returns true until the input has been exhausted.

next

Added in API level 1
open fun next(): Char

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'.

next

Added in API level 1
open fun next(c: Char): Char

Returns the next available character if it equals c. Otherwise an exception is thrown.

next

Added in API level 1
open fun next(length: Int): String!

Returns the next length characters of the input.

Exceptions
org.json.JSONException if the remaining input is not long enough to satisfy this request.

nextClean

Added in API level 1
open fun nextClean(): Char

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'.

nextString

Added in API level 1
open fun nextString(quote: Char): String!

Returns the string up to but not including 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.

Parameters
quote Char: either ' or ".

nextTo

Added in API level 1
open fun nextTo(excluded: String!): String!

Returns the trimmed string holding the characters up to but not including the first of:

  • any character in excluded
  • a newline character '\n'
  • a carriage return '\r'

Return
String! a possibly-empty string

nextTo

Added in API level 1
open fun nextTo(excluded: Char): String!

Equivalent to nextTo(String.valueOf(excluded)).

nextValue

Added in API level 1
open fun nextValue(): Any!

Returns the next value from the input.

Return
Any! a JSONObject, JSONArray, String, Boolean, Integer, Long, Double or JSONObject#NULL.
Exceptions
org.json.JSONException if the input is malformed.

skipPast

Added in API level 1
open fun skipPast(thru: String!): Unit

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

skipTo

Added in API level 1
open fun skipTo(to: Char): Char

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

syntaxError

Added in API level 1
open fun syntaxError(message: String!): JSONException!

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

toString

Added in API level 1
open fun toString(): String

Returns the current position and the entire input string.

Return
String a string representation of the object.