简体   繁体   English

使用gson包解析嵌套的json结构

[英]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. Java中对JSON结构的解码已得到解答,我已将其用于几个简单结构,但这似乎有些棘手。 (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) (我没有花太多时间在开发Java代码上,因此我可能在这里错过了一些特定于语言的东西,我也可能错了)

{
    "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, 上面是提到的结构,我正在使用Gson进行解码,下面是我正在尝试的结构,

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 我在第一个recursive函数调用时在运行时遇到以下错误

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. 我还没有尝试使用JsonParser使用JsonParser 解析嵌套的JSON中定义的方法来尝试这种方法,但是对解码它的任何输入表示赞赏。

Replace java.util.HashMap class with the java.util.Map interface. java.util.Map接口替换java.util.HashMap类。 It's implemented by Gson's LinkedTreeMap so it won't trigger ClassCastException . 它由Gson的LinkedTreeMap实现,因此不会触发ClassCastException

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

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

Gson class LinkedTreeMap extends AbstractHashMap which internally implements Map interface. GsonLinkedTreeMap扩展了内部实现Map接口的AbstractHashMap
Gson uses LinkedTreeMap instead of HashMap . Gson使用LinkedTreeMap代替HashMap Since you are casting the object to HashMap which cannot be done (since it is not present in the hierarchy), hence the ClassCastException 由于您将对象强制转换为HashMap ,这是不可能完成的(因为它不在层次结构中),因此ClassCastException
As mentioned by @Karol, replace the HashMap with Map and everything should work fine. 如@Karol所述,将HashMap替换为Map ,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM