Create a java map using json string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | { "userType":"I", "classList": [ { "id": 1, "name":"c1" }, { "id": 2, "name":"c2" }, { "id": 3, "name":"c3" } ] } |
如何向上面的示例示例声明和放置值?
使用杰克逊图书馆,
对maven使用此依赖项,
1 2 3 4 5 | <dependency> <groupId>com.fasterxml.jackson.core</groupId> jackson-core</artifactId> <version>2.8.7</version> </dependency> |
您需要创建一个Map,并将map和List放入第一个Map:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public Map<String, Object> createMap() { Map<String, Object> map = new HashMap<String, Object>(); map.put("userType","I"); List<Map<String, Object>> classList = new ArrayList<Map<String, Object>>(); Map<String, Object> class1 = new HashMap<String, Object>(); class1.put("id", 1); class1.put("name","c1"); classList.add(class1); Map<String, Object> class2 = new HashMap<String, Object>(); class1.put("id", 2); class1.put("name","c2"); classList.add(class2); Map<String, Object> class3 = new HashMap<String, Object>(); class1.put("id", 3); class1.put("name","c3"); classList.add(class3); map.put("classList", classList); return map; } |
更多这里,做你的研究用Java创建地图。
1 2 3 |