背景
SpringBoot中,使用@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;
}
}