java hashmap to list,将HashMap转换为List

背景

​ Springmvc中,使用@RquestBody注解 hashMap 接收多个参数的json字符串数据,包括一个数组和一个int值。数组中为一个个的对象组成。

问题

​ 使用 map.get("list")方法,并进行强制转换为 List 类型时,导致转换后的 List 中的对象变成了 LinkedHashMap 类型

ce0e739b843129ea752926a1da0dfc6f.png

67fea934e99fbd5b9515506e505bb598.png

​ 当使用foreach遍历 list 中的对象时,抛出类型转换异常

2a7f5d4ecb45db4e8dcfe13b321f967d.png

8ce6eed49c06f1a93fd949f970b09a17.png

解决方法

​ 先将接收到的 hashMap 转换为 json 字符串,然后将得到的 json 字符串转为 list 即可

代码如下:

List physicalList = JsonUtils.json2ListBean(JsonUtils.toJson(map.get("list")), Physical.class);

附上json工具类

import com.fasterxml.jackson.annotation.JsonInclude.Include;

import com.fasterxml.jackson.core.JsonFactory;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonParser.Feature;

import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;

import net.sf.json.JSONArray;

import java.beans.BeanInfo;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.io.IOException;

import java.io.StringWriter;

import java.lang.reflect.Method;

import java.util.*;

/**

* Json工具类

*/

public class JsonUtils {

private static final ObjectMapper mapper = new ObjectMapper();

static {

mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

mapper.setSerializationInclusion(Include.NON_NULL);

}

private JsonUtils() {

}

/**

* json字符串转换为类

*/

public static T toBean(String json, Class clazz) {

try {

T bean = (T) mapper.readValue(json, clazz);

return bean;

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

@SuppressWarnings("unchecked")

public static HashMap toBean(String json) {

return toBean(json, HashMap.class);

}

@SuppressWarnings("unchecked")

public static HashMap toBeanStr(String json) {

return toBean(json, HashMap.class);

}

@SuppressWarnings("unchecked")

public static T toBean(String json, TypeReference tr){

try {

T bean = (T) mapper.readValue(json, tr);

return bean;

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 对象转换为json字符串

*/

public static String toJson(Object bean) {

String json = null;

JsonGenerator gen = null;

StringWriter sw = new StringWriter();

try {

gen = new JsonFactory().createGenerator(sw);

mapper.writeValue(gen, bean);

json = sw.toString();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (gen != null) {

gen.close();

}

if (sw != null) {

sw.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return json;

}

@SuppressWarnings("unchecked")

public static List toList(String json) {

return toBean(json, ArrayList.class);

}

/**

* 对象转换为Map

*/

public static Map transBean2Map(Object obj) {

if (obj == null) {

return null;

}

Map map = new HashMap();

try {

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor property : propertyDescriptors) {

String key = property.getName();

if (!key.equals("class")) {

Method getter = property.getReadMethod();

Object value = getter.invoke(obj);

map.put(key, value);

}

}

} catch (Exception e) {

System.out.println("transBean2Map Error " + e);

}

return map;

}

/**

* json字符串转换为List

*/

public static Listjson2ListBean(String json,Classcls){

JSONArray jArray= JSONArray.fromObject(json);

Collection collection = JSONArray.toCollection(jArray, cls);

List list = new ArrayList();

Iterator it = collection.iterator();

while (it.hasNext()) {

T bean = (T) it.next();

list.add(bean);

}

return list;

}

}

标签:return,HashMap,List,json,static,import,转换,public,String

来源: https://www.cnblogs.com/zhuang229/p/11656107.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值