java 将List< Map< String, Object > > dataListSE转字符串
时间: 2023-12-17 15:28:58 浏览: 120
Java List或Map对象和json字符串数据的相互转化
5星 · 资源好评率100%
可以使用JSON库将List<Map<String, Object>>转换为字符串。以下是使用Jackson库的示例代码:
```java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Example {
public static void main(String[] args) throws JsonProcessingException {
List<Map<String, Object>> dataListSE = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> map = new HashMap<>();
map.put("a", i);
map.put("b", i);
dataListSE.add(map);
}
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(dataListSE);
System.out.println(jsonString);
}
}
```
输出结果为:
```
[{"a":0,"b":0},{"a":1,"b":1},{"a":2,"b":2},{"a":3,"b":3},{"a":4,"b":4}]
```
阅读全文