package com.moon.server.helper; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.LinkedHashMap; import java.util.List; @SuppressWarnings("ALL") public class JsonHelper { @SuppressWarnings("unchecked") public static LinkedHashMap json2Map(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper.readValue(json, LinkedHashMap.class); } public static String map2Json(Object obj) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper.writeValueAsString(obj); } @SuppressWarnings("unchecked") public static List> json2ListMap(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper.readValue(json, List.class); } public static T json2Object(String json, Class valueType) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper.readValue(json, valueType); } public static T obj2Vo(Object obj, Class valueType) { return JSONObject.parseObject(JSONObject.toJSONString(obj), valueType); } }