将json 数组中数据转化为map
时间: 2023-08-11 15:06:57 浏览: 70
好的,可以使用以下代码将 JSON 数组转化为 Map:
```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
public class JsonUtil {
private static final ObjectMapper objectMapper = new ObjectMapper();
public static Map<String, Object> convertJsonToMap(String json) throws Exception {
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
}
public static <T> Map<String, T> convertJsonToMap(String json, Class<T> clazz) throws Exception {
return objectMapper.readValue(json, new TypeReference<Map<String, T>>() {});
}
public static <T> List<T> convertJsonToList(String json, Class<T> clazz) throws Exception {
return objectMapper.readValue(json, new TypeReference<List<T>>() {});
}
}
```
以上代码使用了 Jackson 库来处理 JSON 数据,其中 `convertJsonToMap` 方法将 JSON 数据转化为 `Map` 对象。如果 JSON 数据中的数据类型是不确定的,可以使用第一个方法,如果数据类型是确定的,可以使用第二个方法并传入具体的类型参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)