在Java中,将List
首先,我们需要导入Jackson库的依赖项。我们可以在Maven中添加以下依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10</version>
</dependency>
其次,我们需要创建一个List
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("name", "Tom");
map1.put("age", 20);
map1.put("gender", "male");
list.add(map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("name", "Lucy");
map2.put("age", 18);
map2.put("gender", "female");
list.add(map2);
接下来,我们需要创建一个ObjectMapper对象来处理转换。我们可以使用以下代码实现:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(list);
System.out.println(json);
以上代码将List
[{"name":"Tom","age":20,"gender":"male"},{"name":"Lucy","age":18,"gender":"female"}]
在以上代码中,我们使用了writeValueAsString()方法将List
在实际开发中,将List