public class JSONTokener extends Object
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:
//
or #
and ending with a
newline character.
/*
and ending with *
/
. Such
comments may not be nested.
'single quoted'
.
0x
or 0X
.
0
.
;
.
=
or =>
.
;
.
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.
Constructor and Description |
---|
JSONTokener(String in) |
Modifier and Type | Method and Description |
---|---|
void |
back() |
static int |
dehexchar(char hex) |
boolean |
more() |
char |
next() |
char |
next(char c) |
String |
next(int length) |
char |
nextClean() |
String |
nextString(char quote)
Returns the string up to but not including
quote , unescaping any character
escape sequences encountered along the way. |
String |
nextTo(char excluded) |
String |
nextTo(String excluded) |
Object |
nextValue()
Returns the next value from the input.
|
void |
skipPast(String thru) |
char |
skipTo(char to) |
JSONException |
syntaxError(String message)
Returns an exception containing the given message plus the current position and the
entire input string.
|
String |
toString()
Returns the current position and the entire input string.
|
public JSONTokener(String in)
in
- JSON encoded string. Null is not permitted and will yield a tokener that
throws NullPointerExceptions
when methods are called.public Object nextValue() throws JSONException
JSONObject
, JSONArray
, String, Boolean, Integer, Long,
Double or JSONObject.NULL
.JSONException
- if the input is malformed.public String nextString(char quote) throws JSONException
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.quote
- either ' or ".quote
NumberFormatException
- if any unicode escape sequences are malformed.JSONException
- if processing of json failedpublic JSONException syntaxError(String message)
message
- the messagepublic String toString()
public boolean more()
public char next()
public char next(char c) throws JSONException
JSONException
public char nextClean() throws JSONException
JSONException
public String next(int length) throws JSONException
JSONException
public String nextTo(char excluded)
public void skipPast(String thru)
public char skipTo(char to)
public void back()
public static int dehexchar(char hex)