Search code examples
javajsongson

Gson, JSON and the subtleties of LinkedTreeMap


I've recently started playing around with JSON strings, and was told that Google's own library, Gson, is the new and hip way of dealing with these.

The way I've understood it, is that a JSON string is essentially a map. Where each variable points to a value in the string.

For example:

String jsonInput2 = "{\"created_at\":\"Sat Feb 08 15:37:37 +0000 2014\",\"id\":432176397474623489\"}

Thus far, all is well. Information such as when this JSON string was created, can be assigned to a variable with the following code:

Gson gson = new Gson();

Map<String, String> map = new HashMap<String, String>();

map = (Map<String, String>) gson.fromJson(jsonInput, map.getClass());

String createdAt = map.get("created_at");

It's almost artistic in in simple beauty. But this is where the beauty ends and my confusion begins.

The following is an extension of the above JSON string;

String jsonInput2 = "{\"created_at\":\"Sat Feb 08 15:37:37 +0000 2014\",\"id\":432176397474623489\",\"user\":{\"id_str\":\"366301747\",\"name\":\"somethingClever\",\"screen_name\":\"somethingCoolAndClever\"}}";

My question is how these "brackets within brackets" work for the user section of the JSON?

How could I assign the values specified within these inner-brackets to variables?

Can anyone explain to me, or show me in code, how Gson handles stuff like this, and how I can use it?

In short, why does...

String jsonInput = "{\"created_at\":\"Sat Feb 08 15:37:37 +0000 2014\",\"id\":432176397474623489\",\"user\":{\"id_str\":\"366301747\",\"name\":\"somethingClever\",\"screen_name\":\"somethingCoolAndClever\"}}";

Gson gson = new Gson();

Map<String, String> map = new HashMap<String, String>();

map = (Map<String, String>) gson.fromJson(jsonInput, map.getClass());

String name = map.get("name");

System.out.println(name);

... print out null?


Solution

  • Forget about Java. You need to first understand the JSON format.

    This is basically it

    object
        {}
        { members }
    members
        pair
        pair , members
    pair
        string : value
    array
        []
        [ elements ]
    elements
        value 
        value , elements
    value
        string
        number
        object
        array
        true
        false
        null
    

    Your second JSON String (which has a missing ") is the following (use https://jsonlint.com/ to format)

    {
        "created_at": "Sat Feb 08 15:37:37 +0000 2014",
        "id": "432176397474623489",
        "user": {
            "id_str": "366301747",
            "name": "somethingClever",
            "screen_name": "somethingCoolAndClever"
        }
    }
    

    The JSON is an object, outer {}, that contains three pairs, created_at which is a JSON string, id which is also a JSON string, and user which is a JSON object. That JSON object contains three more pairs which are all JSON strings.

    You asked

    How could I assign the values specified within these inner-brackets to variables?

    Most advanced JSON parsing/generating libraries are meant to convert JSON to Pojos and back.

    So you could map your JSON format to Java classes.

    class Pojo {
        @SerializedName("created_at")
        private String createdAt;
        private String id;
        private User user;
    }
    
    class User {
        @SerializedName("id_str")
        private String idStr;
        private String name;
        @SerializedName("screen_name")
        private String screenName;
    }
    
    // with appropriate getters, setters, and a toString() method
    

    Note the @SerializedName (JavaDoc) so that you can keep using Java naming conventions for your fields.

    You can now deserialize your JSON

    Gson gson = new Gson();
    Pojo pojo = gson.fromJson(jsonInput2, Pojo.class);
    System.out.println(pojo);
    

    would print

    Pojo [createdAt=Sat Feb 08 15:37:37 +0000 2014, id=432176397474623489, user=User [idStr=366301747, name=somethingClever, screenName=somethingCoolAndClever]]
    

    showing that all the fields were set correctly.

    Can anyone explain to me, or show me in code, how Gson handles stuff like this, and how I can use it?

    The source code of Gson is freely available. You can find it online. It is complex and a source code explanation wouldn't fit here. Simply put, it uses the Class object you provide to determine how it will map the JSON pairs. It looks at the corresponding class's fields. If those fields are other classes, then it recurs until it has constructed a map of everything it needs to deserialize.


    In short, why does...print out null?

    Because your root JSON object, doesn't have a pair with name name. Instead of using Map, use Gson's JsonObject type.

    JsonObject jsonObject = new Gson().fromJson(jsonInput2, JsonObject.class);
    
    String name = jsonObject.get("user")       // get the 'user' JsonElement
                            .getAsJsonObject() // get it as a JsonObject
                            .get("name")       // get the nested 'name' JsonElement
                            .getAsString();    // get it as a String
    System.out.println(name);
    

    which prints

    somethingClever
    

    The above method class could have thrown a number of exceptions if they weren't the right type. If, for example, we had done

    String name = jsonObject.get("user")       // get the 'user' JsonElement
                            .getAsJsonArray()  // get it as a JsonArray
    

    it would fail because user is not a JSON array. Specifically, it would throw

    Exception in thread "main" java.lang.IllegalStateException: This is not a JSON Array.
        at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
        at com.spring.Example.main(Example.java:19)
    

    So the JsonElement (JavaDoc) class (which is the parent class of JsonObject, JsonArray, and a few others) provides methods to check what it is. See the javadoc.