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
//
orand 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
or0X
. - 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 | |
---|---|
JSONTokener(in: String!) |
Public methods | |
---|---|
open Unit |
back() Unreads the most recent character of input. |
open static Int |
Returns the integer [0. |
open Boolean |
more() Returns true until the input has been exhausted. |
open Char |
next() Returns the next available character, or the null character '\0' if all input has been exhausted. |
open Char |
Returns the next available character if it equals |
open String! |
Returns the next |
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 |
open String! |
Returns the
|
open String! |
Equivalent to |
open Any! |
Returns the next value from the input. |
open Unit |
Advances past all input up to and including the next occurrence of |
open Char |
Advances past all input up to but not including the next occurrence of |
open JSONException! |
syntaxError(message: String!) Returns an exception containing the given message plus the current position and the entire input string. |
open String |
toString() Returns the current position and the entire input string. |
Public constructors
JSONTokener
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
open fun back(): Unit
Unreads the most recent character of input. If no input characters have been read, the input is unchanged.
dehexchar
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. |
next
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
open fun next(c: Char): Char
Returns the next available character if it equals c
. Otherwise an exception is thrown.
next
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
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
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
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
open fun nextTo(excluded: Char): String!
Equivalent to nextTo(String.valueOf(excluded))
.
nextValue
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
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
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
open fun syntaxError(message: String!): JSONException!
Returns an exception containing the given message plus the current position and the entire input string.
toString
open fun toString(): String
Returns the current position and the entire input string.
Return | |
---|---|
String |
a string representation of the object. |