我有以下JSON数据:
JSON:
[{
"valInternalCode": "NE",
"valMinimumInputString": "NE",
"valExternalRepresentation": "Northeast",
"valActionCode1": "1",
"valActionCode2": null,
"valActionCode3": null,
"valActionCode4": null,
"id": {
"valcodeId": "X.LOCATION",
"pos": 1
},
"uniqueId": "X.LOCATION-1",
"caption": "Northeast"
}, {
"valInternalCode": "NW",
"valMinimumInputString": "NW",
"valExternalRepresentation": "Northwest",
"valActionCode1": "1",
"valActionCode2": null,
"valActionCode3": null,
"valActionCode4": null,
"id": {
"valcodeId": "X.LOCATION",
"pos": 2
},
"uniqueId": "X.LOCATION-2",
"caption": "Northwest"
}
我能够解析它并从数据中创建一个Map,如下所示:
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<Map>>(){}.getType();
Collection<Map> dataCollection = gson.fromJson(jsonString.toString(), collectionType);
然后我可以迭代它并使用Key得到一个我需要的值:
iterator.next().get("valInternalCode");
我正在努力解决的问题是如何获得id字段内的内容:
"id": {
"valcodeId": "X.LOCATION",
"pos": 2
},
我正在使用Hibernate从非规范化的Oracle数据库中获取数据(这就是为什么hibernate以它的方式创建id字段的原因)
编辑:
我尝试以丑陋的方式做这件事。基本上在循环中循环:
while (valIterator.hasNext()) {
Map currentVal = valIterator.next();
String valId = "";
Collection<Map> idVal = (Collection<Map>) currentVal.get("id");
Iterator<Map> valIdIterator = idVal.iterator();
while (valIdIterator.hasNext()) {
Map valIdCurrentVal = valIdIterator.next();
valId = valIdCurrentVal.get("valcodeId").toString();
}
}
当我尝试将currentVal.get(&#34; id&#34;)转换为集合时获取ClassCastException