NEB
NEB

Reputation: 734

Parsing a nested json structure using gson package

The decoding of the JSON structure in Java has already been answered and I've used it for couple of simple structure, but this seems a little bit tricky. (I've not spent too much time developing Java code, so I may be missing some thing specific to language here and I may be wrong on that too)

{
    "who": "123",
    "mapping": {
        "element1": {
            "element11": {
                "relation": "rel1",
                "direction_p2c": true,
                "element111": {
                    "relation": "rel2",
                    "direction_p2c": true,
                    "element1111": {
                        "relation": "rel1",
                        "direction_p2c": false
                    },
                    "element1112": {
                        "relation": "rel1",
                        "direction_p2c": false
                    },
                    "element1113": {
                        "relation": "rel1",
                        "direction_p2c": false
                    }
                },
                "element112": {
                    "relation": "rel3",
                    "direction_p2c": false
                }
            },
            "element12": {
                "relation": "rel1",
                "direction_p2c": true,
                "element121": {
                    "relation": "rel2",
                    "direction_p2c": true,
                    "element1211": {
                        "relation": "rel1",
                        "direction_p2c": false
                    }
                }
            }
        },
        "element2": {
            "element21": {
                "relation": "rel2",
                "direction_p2c": true
            },
            "element22": {
                "relation": "rel3",
                "direction_p2c": true
            },
            "element23": {
                "relation": "rel1",
                "direction_p2c": true
            }
        }
    }
}

Above is the mentioned structure and I'm using Gson for decoding, below is what I'm trying with,

void recursive(String key, HashMap<String, Object> value) {
    for (Map.Entry<String, Object> entry: value.entrySet()) {
        recurssive(entry.getKey(), (HashMap<String, Object>) entry.getValue());
    }
}

HashMap<String, Object> decodedJson = new Gson().fromJson(inputJson, HashMap.class);
recursive("mapping", (HashMap<String, Object>) decodedJson.get("mapping"));

And I'm getting the below error in runtime at the first recursive function call

javax.servlet.ServletException: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap

I'm yet to try this with the way defined in Parse a nested JSON using gson using JsonParser, but any input in decoding this is appreciated.

Upvotes: 2

Views: 268

Answers (2)

swayamraina
swayamraina

Reputation: 3158

Gson class LinkedTreeMap extends AbstractHashMap which internally implements Map interface.
Gson uses LinkedTreeMap instead of HashMap. Since you are casting the object to HashMap which cannot be done (since it is not present in the hierarchy), hence the ClassCastException
As mentioned by @Karol, replace the HashMap with Map and everything should work fine.

Upvotes: 1

Karol Dowbecki
Karol Dowbecki

Reputation: 44980

Replace java.util.HashMap class with the java.util.Map interface. It's implemented by Gson's LinkedTreeMap so it won't trigger ClassCastException.

void recursive(String key, Map<String, Object> value) {
  ...
}

Map<String, Object> decodedJson = new Gson().fromJson(inputJson, Map.class);
...

Upvotes: 3

Related Questions