Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/org/json/JSONTokener#pubctors) \| [Methods](https://developer.android.com/reference/org/json/JSONTokener#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/org/json/JSONTokener#inhmethods)

# JSONTokener


`
public


class
JSONTokener
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | org.json.JSONTokener |

<br />

*** ** * ** ***

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 `https://developer.android.com/reference/org/json/JSONTokener#JSONTokener(java.lang.String)`
and `https://developer.android.com/reference/org/json/JSONTokener#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 `https://developer.android.com/reference/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 `//` 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 ||
|---|---|
| ` https://developer.android.com/reference/org/json/JSONTokener#JSONTokener(java.lang.String)(https://developer.android.com/reference/java/lang/String in) ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/org/json/JSONTokener#back()() ` Unreads the most recent character of input. |
| ` static int` | ` https://developer.android.com/reference/org/json/JSONTokener#dehexchar(char)(char hex) ` Returns the integer \[0..15\] value for the given hex character, or -1 for non-hex input. |
| ` boolean` | ` https://developer.android.com/reference/org/json/JSONTokener#more()() ` Returns true until the input has been exhausted. |
| ` char` | ` https://developer.android.com/reference/org/json/JSONTokener#next()() ` Returns the next available character, or the null character '\\0' if all input has been exhausted. |
| ` char` | ` https://developer.android.com/reference/org/json/JSONTokener#next(char)(char c) ` Returns the next available character if it equals `c`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/org/json/JSONTokener#next(int)(int length) ` Returns the next `length` characters of the input. |
| ` char` | ` https://developer.android.com/reference/org/json/JSONTokener#nextClean()() ` Returns the next character that is not whitespace and does not belong to a comment. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/org/json/JSONTokener#nextString(char)(char quote) ` Returns the string up to but not including `quote`, unescaping any character escape sequences encountered along the way. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/org/json/JSONTokener#nextTo(java.lang.String)(https://developer.android.com/reference/java/lang/String excluded) ` Returns the `https://developer.android.com/reference/java/lang/String#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 /> |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/org/json/JSONTokener#nextTo(char)(char excluded) ` Equivalent to `nextTo(String.valueOf(excluded))`. |
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/org/json/JSONTokener#nextValue()() ` Returns the next value from the input. |
| ` void` | ` https://developer.android.com/reference/org/json/JSONTokener#skipPast(java.lang.String)(https://developer.android.com/reference/java/lang/String thru) ` Advances past all input up to and including the next occurrence of `thru`. |
| ` char` | ` https://developer.android.com/reference/org/json/JSONTokener#skipTo(char)(char to) ` Advances past all input up to but not including the next occurrence of `to`. |
| ` https://developer.android.com/reference/org/json/JSONException` | ` https://developer.android.com/reference/org/json/JSONTokener#syntaxError(java.lang.String)(https://developer.android.com/reference/java/lang/String message) ` Returns an exception containing the given message plus the current position and the entire input string. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/org/json/JSONTokener#toString()() ` Returns the current position and the entire input string. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### JSONTokener

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

```
public JSONTokener (String in)
```

<br />

<br />

| Parameters ||
|---|---|
| `in` | `String`: JSON encoded string. Null is not permitted and will yield a tokener that throws `NullPointerExceptions` when methods are called. <br /> |

## Public methods

### back

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

```
public void back ()
```

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

<br />

### dehexchar

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

```
public static int dehexchar (char hex)
```

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

<br />

| Parameters ||
|---|---|
| `hex` | `char`: a character in the ranges \[0-9\], \[A-F\] or \[a-f\]. Any other character will yield a -1 result. <br /> |

| Returns ||
|---|---|
| `int` | <br /> |

### more

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

```
public boolean more ()
```

Returns true until the input has been exhausted.

<br />

| Returns ||
|---|---|
| `boolean` | <br /> |

### next

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

```
public char 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'.

<br />

| Returns ||
|---|---|
| `char` | <br /> |

### next

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

```
public char next (char c)
```

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

<br />

| Parameters ||
|---|---|
| `c` | `char` <br /> |

| Returns ||
|---|---|
| `char` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/org/json/JSONException` |   |

### next

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

```
public String next (int length)
```

Returns the next `length` characters of the input.

<br />

| Parameters ||
|---|---|
| `length` | `int` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/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#ApiLevels)

```
public char 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'.

<br />

| Returns ||
|---|---|
| `char` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/org/json/JSONException` |   |

### nextString

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

```
public String nextString (char quote)
```

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.

<br />

| Parameters ||
|---|---|
| `quote` | `char`: either ' or ". <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/org/json/JSONException` |   |

### nextTo

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

```
public String nextTo (String excluded)
```

Returns the `https://developer.android.com/reference/java/lang/String#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 />

<br />

| Parameters ||
|---|---|
| `excluded` | `String` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a possibly-empty string <br /> |

### nextTo

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

```
public String nextTo (char excluded)
```

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

<br />

| Parameters ||
|---|---|
| `excluded` | `char` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | <br /> |

### nextValue

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

```
public Object nextValue ()
```

Returns the next value from the input.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Object` | a `https://developer.android.com/reference/org/json/JSONObject`, `https://developer.android.com/reference/org/json/JSONArray`, String, Boolean, Integer, Long, Double or `https://developer.android.com/reference/org/json/JSONObject#NULL`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/org/json/JSONException` | if the input is malformed. |

### skipPast

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

```
public void skipPast (String thru)
```

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.

<br />

| Parameters ||
|---|---|
| `thru` | `String` <br /> |

### skipTo

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

```
public char skipTo (char to)
```

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.

<br />

| Parameters ||
|---|---|
| `to` | `char` <br /> |

| Returns ||
|---|---|
| `char` | <br /> |

### syntaxError

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

```
public JSONException syntaxError (String message)
```

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

<br />

| Parameters ||
|---|---|
| `message` | `String` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/org/json/JSONException` | <br /> |

### toString

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

```
public String toString ()
```

Returns the current position and the entire input string.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |