首页 > 其他 > 详细

将HashMap转换为List

时间:2019-10-11 19:40:49      阅读:787      评论:0      收藏:0      [点我收藏+]

背景

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

问题

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

技术分享图片

技术分享图片

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

技术分享图片

技术分享图片

解决方法

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

代码如下:

List<Physical> 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> T toBean(String json, Class<T> clazz) {
        try {
            T bean = (T) mapper.readValue(json, clazz);
            return bean;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public static HashMap<String, Object> toBean(String json) {
        return toBean(json, HashMap.class);
    }

    @SuppressWarnings("unchecked")
    public static HashMap<String,String> toBeanStr(String json) {
        return toBean(json, HashMap.class);
    }

    @SuppressWarnings("unchecked")
    public static <T> T toBean(String json, TypeReference<T> 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) throws IOException {
        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) {
            throw e;
        } finally {
            try {
                if (gen != null) {
                    gen.close();
                }
                if (sw != null) {
                    sw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return json;
    }

    @SuppressWarnings("unchecked")
    public static List<Object> toList(String json) {
        return toBean(json, ArrayList.class);
    }

    /**
     * 对象转换为Map
     */
    public static Map<String, Object> transBean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        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 <T> List<T>json2ListBean(String json,Class<T>cls){
        JSONArray jArray= JSONArray.fromObject(json);
        Collection<T> collection = JSONArray.toCollection(jArray, cls);
        List<T> list = new ArrayList<T>();
        Iterator<T> it = collection.iterator();
        while (it.hasNext()) {
            T bean = (T) it.next();
            list.add(bean);
        }
        return list;
    }
}

将HashMap转换为List

原文:https://www.cnblogs.com/zhuang229/p/11656107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!