{
"userType": "I",
"classList": [
{
"id": 1,
"name": "c1"
},
{
"id": 2,
"name": "c2"
},
{
"id": 3,
"name": "c3"
}
]
}
如何向上面的示例示例声明和放置值?
答案 0 :(得分:2)
使用jackson库,
HashMap<String,Object> map =
new ObjectMapper().readValue(jsonString, HashMap.class);
将此依赖关系用于maven,
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
答案 1 :(得分:0)
更多信息,请进行研究Create Map in Java。
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("id",3);
map.put("name","c2");
答案 2 :(得分:0)
您需要创建一个地图,并将地图和列表放入第一个地图:
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;
}