將HashMap轉換爲List

背景

​ SpringBoot中,使用@RquestBody註解 hashMap 接收多個參數的json字符串數據,包括一個數組和一個int值。數組中爲一個個的對象組成。 <br/>java

問題

​ 使用 **map.get("list") **方法,並進行強制轉換爲 List 類型時,致使轉換後的 List 中的對象變成了 LinkedHashMap 類型json

​ 當使用 foreach 遍歷 List 中的對象時,拋出類型轉換異常數組

<br/> ## 解決方法app

​ 先將接收到的 hashMap 轉換爲 json 字符串,而後將獲得的 json 字符串轉爲 list 便可工具

代碼以下:spa

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

<br/> ## 附上json工具類code

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;
    }
}

<br> <h3>**若是以爲這篇文章對你有幫助,就給個 推薦 吧!**<h3/>xml

相關文章
相關標籤/搜索