HttpResult转map
时间: 2023-10-12 18:13:20 浏览: 61
HttpResult是一个自定义的Java类,如果想要将其转换成Map类型,可以按照以下步骤进行:
1. 创建一个Map对象,用于存储转换后的数据。
2. 获取HttpResult对象中的所有属性值,通过反射获取。
3. 将属性名作为Map中的key,属性值作为Map中的value,存储到Map对象中。
4. 返回转换后的Map对象。
下面是一个示例代码:
```java
public Map<String, Object> convertHttpResultToMap(HttpResult httpResult) throws IllegalAccessException {
Map<String, Object> resultMap = new HashMap<>();
// 获取HttpResult对象的所有属性
Field[] fields = httpResult.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String fieldName = field.getName();
Object fieldValue = field.get(httpResult);
resultMap.put(fieldName, fieldValue);
}
return resultMap;
}
```
调用方式如下:
```java
HttpResult httpResult = new HttpResult();
// 设置HttpResult对象的属性值
Map<String, Object> resultMap = convertHttpResultToMap(httpResult);
```
注意:该方法只能将HttpResult对象中的属性转换成Map,如果HttpResult对象中包含其他对象类型的属性,需要递归处理。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)