String类型转换成List<Map<String,Object>>
时间: 2024-03-12 14:40:45 浏览: 40
可以使用以下方法将String类型转换为List<Map<String, Object>>:
1. 使用JSON库进行转换:
```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
String jsonString = "[{\"key1\":\"value1\",\"key2\":\"value2\"},{\"key1\":\"value3\",\"key2\":\"value4\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, Object>> resultList = objectMapper.readValue(jsonString, new TypeReference<List<Map<String, Object>>>(){});
// 输出结果
System.out.println(resultList);
```
2. 使用Gson库进行转换:
```java
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
String jsonString = "[{\"key1\":\"value1\",\"key2\":\"value2\"},{\"key1\":\"value3\",\"key2\":\"value4\"}]";
Gson gson = new Gson();
List<Map<String, Object>> resultList = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>(){}.getType());
// 输出结果
System.out.println(resultList);
```
3. 使用Jackson库进行转换:
```java
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
String jsonString = "[{\"key1\":\"value1\",\"key2\":\"value2\"},{\"key1\":\"value3\",\"key2\":\"value4\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, Object>> resultList = objectMapper.readValue(jsonString, new TypeReference<List<Map<String, Object>>>(){});
// 输出结果
System.out.println(resultList);
```
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)