我对JSON-Simple的一个抱怨是,如果你有一个严重嵌套的结构,那么访问信息会变得非常冗长。
考虑一个简单的JSON对象:
{
"announcements": {
"inGame": {
"playerDied": "{arg1} has died"
}
}
}
我是否应该打印出“{arg1}已经死亡”,正如我目前所理解的那样,我必须执行以下操作:
InputStreamReader inputStreamReader =
new InputStreamReader(getClass().getResourceAsStream(configurationPath));
JSONObject jsonObject = (JSONObject) parser.parse(inputStreamReader);
String died = (String)((JSONObject)((JSONObject)jsonObject.get("announcements")).get("inGame")).get("playerDied");
System.out.println(died);
正如你所看到的,大量的铸造和许多链接。
我的问题是:有更简单的方法吗?
例如:
String died = jsonObject.get("announcements").get("inGame").get("playerDied");
或者,甚至更好:
String died = jsonObject.get("announcements.inGame.playerDied");
我觉得我错过了什么。
答案 0 :(得分:0)