What is JSON?
JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it.
JSON Reader:
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 a JsonReader.
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 when hasNext() is false. Finally, read the array's closing bracket by calling endArray().
- 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 when hasNext() is false. Finally, read the object's closing brace by calling endObject().
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 otherwise conflict.
If a value may be null, you should first check using peek(). Null literals can be consumed using either nextNull() or skipValue().
This code implements the parser for the universal JSON structure:
import android.util.JsonReader; import android.util.JsonToken; import android.util.Log; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Hashtable; public class JsonParser { Hashtable hashtable; JsonParser(){ hashtable = new Hashtable(); } void parseJSON(InputStream in) throws IOException { readJsonStream(in); } void parseStream(String jsonString) throws IOException { InputStream is = new ByteArrayInputStream(jsonString.getBytes()); parseJSON(is); } void readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); parseContent(reader); reader.close(); } void parseContent(JsonReader reader) throws IOException { JsonToken jsonToken = reader.peek(); if (jsonToken.equals(JsonToken.BEGIN_ARRAY)) { readJSONArray(reader, jsonToken.name()); } else if (jsonToken.equals(JsonToken.BEGIN_OBJECT)) { readJSONObject(reader, jsonToken.name()); } } synchronized private void readJSONArray(JsonReader reader, String identifier) throws IOException { reader.beginArray(); while (reader.hasNext()) { readJSONObject(reader, identifier); } reader.endArray(); } synchronized private void readJSONObject(JsonReader reader, String identifier) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); JsonToken jsonToken = reader.peek(); if (jsonToken.equals(JsonToken.BEGIN_ARRAY)) { readJSONArray(reader, name); } else if (jsonToken.equals(JsonToken.BEGIN_OBJECT)) { readJSONObject(reader, name); } else { addAttributes(name,reader, jsonToken.name(), jsonToken); } } reader.endObject(); } synchronized private void addAttributes(String attribute,JsonReader reader, String identifier,JsonToken jsonToken) throws IOException { String value; try { value = reader.nextString(); hashtable.put(attribute,value); Log.d("addAttributes","Attribute"+attribute+"Value:::"+value); } catch (Exception e) { reader.skipValue(); } } }
Use the following code snippet to parse JSON InputStream
JsonParser jsonParser = new JsonParser(); try { jsonParser.readJsonStream(is); } catch (IOException e) { e.printStackTrace(); }