# JSONTokener

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# JSONTokener

```
open class JSONTokener
```

|---|----------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)               ||
| ↳ | [org.json.JSONTokener](https://developer.android.com/reference/kotlin/org/json/JSONTokener#) |

Parses a JSON ([RFC 4627](http://www.ietf.org/rfc/rfc4627.txt)) encoded string into the corresponding object. Most clients of this class will use only need the[constructor](https://developer.android.com/reference/kotlin/org/json/JSONTokener#JSONTokener(kotlin.String))and[nextValue](https://developer.android.com/reference/kotlin/org/json/JSONTokener#nextValue())method. Example usage:  

```kotlin
String json = "{"
          + "  \"query\": \"Pizza\", "
          + "  \"locations\": [ 94043, 90210 ] "
          + "}";
 
  JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
  String query = object.getString("query");
  JSONArray locations = object.getJSONArray("locations");
```

<br />

For best interoperability and performance use JSON that complies with RFC 4627, such as that generated by[JSONStringer](https://developer.android.com/reference/kotlin/org/json/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`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                                                                                                  ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [JSONTokener](https://developer.android.com/reference/kotlin/org/json/JSONTokener#JSONTokener(kotlin.String))`(`in:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!`)` <br /> |

|                                                                                                                                                                                                                                                                           Public methods                                                                                                                                                                                                                                                                           ||
|---------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)            | [back](https://developer.android.com/reference/kotlin/org/json/JSONTokener#back())`()` Unreads the most recent character of input.                                                                                                                                                                                                                                                                                                                                    |
| openstatic[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)        | [dehexchar](https://developer.android.com/reference/kotlin/org/json/JSONTokener#dehexchar(kotlin.Char))`(`hex:` `[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)`)` Returns the integer \[0.                                                                                                                                                                                                                                             |
| open[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)      | [more](https://developer.android.com/reference/kotlin/org/json/JSONTokener#more())`()` Returns true until the input has been exhausted.                                                                                                                                                                                                                                                                                                                               |
| open[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)            | [next](https://developer.android.com/reference/kotlin/org/json/JSONTokener#next())`()` Returns the next available character, or the null character '\\0' if all input has been exhausted.                                                                                                                                                                                                                                                                             |
| open[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)            | [next](https://developer.android.com/reference/kotlin/org/json/JSONTokener#next(kotlin.Char))`(`c:` `[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)`)` Returns the next available character if it equals`c`.                                                                                                                                                                                                                            |
| open[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!       | [next](https://developer.android.com/reference/kotlin/org/json/JSONTokener#next(kotlin.Int))`(`length:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Returns the next`length`characters of the input.                                                                                                                                                                                                                               |
| open[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)            | [nextClean](https://developer.android.com/reference/kotlin/org/json/JSONTokener#nextClean())`()` Returns the next character that is not whitespace and does not belong to a comment.                                                                                                                                                                                                                                                                                  |
| open[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!       | [nextString](https://developer.android.com/reference/kotlin/org/json/JSONTokener#nextString(kotlin.Char))`(`quote:` `[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)`)` Returns the string up to but not including`quote`, unescaping any character escape sequences encountered along the way.                                                                                                                                          |
| open[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!       | [nextTo](https://developer.android.com/reference/kotlin/org/json/JSONTokener#nextTo(kotlin.Char))`(`excluded:` `[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)`)` Equivalent to`nextTo(String.valueOf(excluded))`.                                                                                                                                                                                                                      |
| open[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!       | [nextTo](https://developer.android.com/reference/kotlin/org/json/JSONTokener#nextTo(kotlin.String))`(`excluded:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!`)` Returns the[trimmed](https://developer.android.com/reference/kotlin/java/lang/String.html#trim())string holding the characters up to but not including the first of: - any character in`excluded` - a newline character '\\n' - a carriage return '\\r' <br /> |
| open[Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)!             | [nextValue](https://developer.android.com/reference/kotlin/org/json/JSONTokener#nextValue())`()` Returns the next value from the input.                                                                                                                                                                                                                                                                                                                               |
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)            | [skipPast](https://developer.android.com/reference/kotlin/org/json/JSONTokener#skipPast(kotlin.String))`(`thru:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!`)` Advances past all input up to and including the next occurrence of`thru`.                                                                                                                                                                                      |
| open[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)            | [skipTo](https://developer.android.com/reference/kotlin/org/json/JSONTokener#skipTo(kotlin.Char))`(`to:` `[Char](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html)`)` Advances past all input up to but not including the next occurrence of`to`.                                                                                                                                                                                                 |
| open[JSONException](https://developer.android.com/reference/kotlin/org/json/JSONException)! | [syntaxError](https://developer.android.com/reference/kotlin/org/json/JSONTokener#syntaxError(kotlin.String))`(`message:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!`)` Returns an exception containing the given message plus the current position and the entire input string.                                                                                                                                              |
| open[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)        | [toString](https://developer.android.com/reference/kotlin/org/json/JSONTokener#toString())`()` Returns the current position and the entire input string.                                                                                                                                                                                                                                                                                                              |

## Public constructors

### JSONTokener

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
JSONTokener(in: String!)
```

|                                                                                                       Parameters                                                                                                       ||
|------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `in` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!: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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html):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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun more(): Boolean
```

Returns true until the input has been exhausted.  

### next

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/index.html):either ' or ". |

### nextTo

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun nextTo(excluded: Char): String!
```

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

### nextTo

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun nextTo(excluded: String!): String!
```

Returns the[trimmed](https://developer.android.com/reference/kotlin/java/lang/String.html#trim())string holding the characters up to but not including the first of:

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

<br />

|                                                   Return                                                   ||
|-----------------------------------------------------------------------------------|-------------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | a possibly-empty string |

### nextValue

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun nextValue(): Any!
```

Returns the next value from the input.

|                                                                                                                                                                                             Return                                                                                                                                                                                             ||
|-----------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)! | a[JSONObject](https://developer.android.com/reference/kotlin/org/json/JSONObject),[JSONArray](https://developer.android.com/reference/kotlin/org/json/JSONArray), String, Boolean, Integer, Long, Double or[JSONObject.NULL](https://developer.android.com/reference/kotlin/org/json/JSONObject#NULL:kotlin.Any). |

|                      Exceptions                      ||
|--------------------------|----------------------------|
| `org.json.JSONException` | if the input is malformed. |

### skipPast

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun toString(): String
```

Returns the current position and the entire input string.

|                                                          Return                                                          ||
|----------------------------------------------------------------------------------|----------------------------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) | a string representation of the object. |