JsonReader
public
final
class
JsonReader
extends Object
implements
Closeable
| java.lang.Object | |
| ↳ | android.util.JsonReader |
Reads a JSON (RFC 4627) encoded value as a stream of tokens. This stream includes both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays. The tokens are traversed in depth-first order, the same order that they appear in the JSON document. Within JSON objects, name/value pairs are represented by a single token.
Parsing JSON
To create a recursive descent parser for your own JSON streams, first create an entry point method that creates aJsonReader.
Next, create handler methods for each structure in your JSON text. You'll need a method for each object type and for each array type.
- Within array handling methods, first call
beginArray()to consume the array's opening bracket. Then create a while loop that accumulates values, terminating whenhasNext()is false. Finally, read the array's closing bracket by callingendArray(). - Within object handling methods, first call
beginObject()to consume the object's opening brace. Then create a while loop that assigns values to local variables based on their name. This loop should terminate whenhasNext()is false. Finally, read the object's closing brace by callingendObject().
When a nested object or array is encountered, delegate to the corresponding handler method.
When an unknown name is encountered, strict parsers should fail with an
exception. Lenient parsers should call skipValue() to recursively
skip the value's nested tokens, which may othe