我想用Jackson将 JSON 转换为 HashMap 。这是我的 JSON:
String json = "[{\"Opleidingen\":[{\"name\":\"Bijz. trajecten zorg en welzijn\",\"afk\":\"BTZW\",\"id\":\"0\"},{\"name\":\"Bouwkunde\",\"afk\":\"Bwk\",\"id\":\"14\"},{\"name\":\"Electrotechniek / mechatronica\",\"afk\":\"EltMe\",\"id\":\"15\"},{\"name\":\"Extern\",\"afk\":\"Extern\",\"id\":\"16\"},{\"name\":\"Gezondheidszorg\",\"afk\":\"Zorg\",\"id\":\"17\"},{\"name\":\"Handel\",\"afk\":\"Hand\",\"id\":\"18\"},{\"name\":\"Horeca\",\"afk\":\"Hor\",\"id\":\"19\"},{\"name\":\"Ict\",\"afk\":\"ICT\",\"id\":\"20\"},{\"name\":\"Maatschappelijke zorg\",\"afk\":\"MZ\",\"id\":\"21\"},{\"name\":\"Onderwijs assistent / pedagogisch werk\",\"afk\":\"OAPW\",\"id\":\"22\"},{\"name\":\"Tab / brug\",\"afk\":\"TAB\",\"id\":\"23\"},{\"name\":\"Werktuigbouw / maritieme techniek\",\"afk\":\"WtbMt\",\"id\":\"24\"},{\"name\":\"Zakelijke dienstverlening\",\"afk\":\"TAB\",\"id\":\"25\"}]},{\"Klassen\":[{\"name\":\"V2ZWA\",\"cat\":\"BTZW\",\"id\":\"1\"},{\"name\":\"V2ZWB\",\"cat\":\"Bwk\",\"id\":\"2\"},{\"name\":\"V2ZWB\",\"cat\":\"BTZW\",\"id\":\"3\"},{\"name\":\"V3B2A\",\"cat\":\"BTZW\",\"id\":\"3\"},{\"name\":\"V3B2B\",\"cat\":\"BTZW\",\"id\":\"4\"},{\"name\":\"V3B2C\",\"cat\":\"BTZW\",\"id\":\"5\"},{\"name\":\"V3B2D\",\"cat\":\"BTZW\",\"id\":\"6\"},{\"name\":\"V3B2E\",\"cat\":\"BTZW\",\"id\":\"7\"},{\"name\":\"V3B3A\",\"cat\":\"BTZW\",\"id\":\"8\"},{\"name\":\"V3B3B\",\"cat\":\"BTZW\",\"id\":\"9\"},{\"name\":\"V3B3C\",\"cat\":\"BTZW\",\"id\":\"10\"},{\"name\":\"VWA\",\"cat\":\"BTZW\",\"id\":\"11\"},{\"name\":\"VWB\",\"cat\":\"BTZW\",\"id\":\"12\"},{\"name\":\"VWC\",\"cat\":\"BTZW\",\"id\":\"13\"}]}]";
我希望我填写我得到名字的 ID,所以:result.get("13");
返回VWC
和result.get("0");
返回Bijz. trajecten zorg en welzijn
。
我试过这个:
try {
@SuppressWarnings("unchecked")
ArrayList<LinkedHashMap<?, ?>> result = new ObjectMapper().readValue(json, ArrayList.class);
System.out.println(result.get(1).get("id"));
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但这是以下内容result.get(1);
:
I/System.out(809): {Klassen=[{name=V2ZWA, cat=BTZW, id=1}, {name=V2ZWB, cat=Bwk, id=2}, {name=V2ZWB, cat=BTZW, id=3}, {name=V3B2A, cat=BTZW, id=3}, {name=V3B2B, cat=BTZW, id=4}, {name=V3B2C, cat=BTZW, id=5}, {name=V3B2D, cat=BTZW, id=6}, {name=V3B2E, cat=BTZW, id=7}, {name=V3B3A, cat=BTZW, id=8}, {name=V3B3B, cat=BTZW, id=9}, {name=V3B3C, cat=BTZW, id=10}, {name=VWA, cat=BTZW, id=11}, {name=VWB, cat=BTZW, id=12}, {name=VWC, cat=BTZW, id=13}]}
我也试过没有杰克逊:
for (int i = 0; i < jObject.length(); i++) {
JSONArray opleidingen = jObject.getJSONObject(i).getJSONArray("Opleidingen");
for (int i2 = 0; i2 < opleidingen.length(); i2++) {
outMap.put(opleidingen.getJSONObject(i2).getString("id"), opleidingen.getJSONObject(i2).getString("name"));
}
JSONArray klassen = jObject.getJSONObject(i).getJSONArray("Klassen");
for (int i3 = 0; i3 < klassen.length(); i3++) {
outMap.put(klassen.getJSONObject(i3).getString("id"), klassen.getJSONObject(i3).getString("name"));
}
}
但这都行不通。
是否可以将我的 JSON 转换为 HashMap(并且当给出 ID 时它返回名称)?
(ArrayList 类可以在这里找到)