GSON - JsonParser

Jakob Jenkov
Last update: 2021-01-27

The GSON JsonParser class can parse a JSON string or stream into a tree structure of Java objects. GSON also has two other parsers. The Gson JSON parser which can parse JSON into Java objects, and the JsonReader which can parse a JSON string or stream into tokens (a pull parser). This tutorial focuses on the JsonParser though - GSON's tree parser.

Creating a JsonParser

Before you can use the GSON JsonParser you must create a JsonParser instance. Here is an example of creating a JsonParser instance:

JsonParser jsonParser = new JsonParser();

Parsing JSON Into a Tree Structure

Once you have created a JsonParser you can parse JSON into a tree structure with it. Here is an example of parsing a JSON string into a tree structure of GSON objects with the JsonParser:

JsonParser parser = new JsonParser();

String json = "{ \"f1\":\"Hello\",\"f2\":{\"f3\":\"World\"}}";

JsonElement jsonTree = parser.parse(json);

The parsing of the JSON happens in the third line of code, by calling parse() on the JsonParser, passing as parameter a reference to the JSON string (or stream) to parse.

Iterating the JSON Tree Structure

The parsed JSON tree structure consists of objects from the GSON API. The root of a JSON tree structure is a JsonElement object. You can find out what type of JSON element it represents using one of the type checking methods:

jsonTree.isJsonObject();
jsonTree.isJsonArray();
jsonTree.isJsonNull();
jsonTree.isJsonPrimitive();

The JSON string parsed above is a JSON object. Thus, we will expect the JsonElement to represent a JSON object. If it does, we will do something with it. Here is how that looks:

if(jsonTree.isJsonObject()) {
    JsonObject jsonObject = jsonTree.getAsJsonObject();
}

Once you have a JsonObject instance you can extract fields from it using its get() method. Here is an example:

JsonObject jsonObject = jsonTree.getAsJsonObject();

JsonElement f1 = jsonObject.get("f1");

JsonElement f2 = jsonObject.get("f1");

You can inspect the type of each of these fields too, just like with the first JsonElement obtained from the JsonParser parse() method. Here is an example showing how:

if(f2.isJsonObject()){
    JsonObject f2Obj = f2.getAsJsonObject();

    JsonElement f3 = f2Obj.get("f3");
}

Here is a full example showing how to iterate the JsonElement obtained from the JsonReader :

JsonParser parser = new JsonParser();

String json = "{ \"f1\":\"Hello\",\"f2\":{\"f3\":\"World\"}}";

JsonElement jsonTree = parser.parse(json);

if(jsonTree.isJsonObject()){
    JsonObject jsonObject = jsonTree.getAsJsonObject();

    JsonElement f1 = jsonObject.get("f1");

    JsonElement f2 = jsonObject.get("f2");

    if(f2.isJsonObject()){
        JsonObject f2Obj = f2.getAsJsonObject();

        JsonElement f3 = f2Obj.get("f3");
    }

}

Jakob Jenkov

Featured Videos















Core Software Performance Optimization Principles




Advertisements

High-Performance
Java Persistence
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next