java中JSON字符串转成List<Map>
时间: 2024-05-08 13:19:06 浏览: 99
可以使用Jackson库来实现JSON字符串转成List<Map>,具体代码如下:
```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class JsonUtils {
private static final ObjectMapper mapper = new ObjectMapper();
public static List<Map<String, Object>> jsonToListMap(String jsonStr) {
try {
return mapper.readValue(jsonStr, new TypeReference<List<Map<String, Object>>>() {});
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
```
使用示例:
```java
String jsonStr = "[{\"id\":1,\"name\":\"Tom\"},{\"id\":2,\"name\":\"Jerry\"}]";
List<Map<String, Object>> listMap = JsonUtils.jsonToListMap(jsonStr);
for (Map<String, Object> map : listMap) {
System.out.println(map.get("id") + " " + map.get("name"));
}
```
输出结果:
```
1 Tom
2 Jerry
```
阅读全文