Created
July 9, 2019 03:19
-
-
Save qlong8807/898c551492cb9d3f761e7777b6a15e2f to your computer and use it in GitHub Desktop.
json to list map bean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.alibaba.fastjson.JSON; | |
import com.fasterxml.jackson.databind.JavaType; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import net.sf.json.JSONArray; | |
import net.sf.json.JSONObject; | |
import net.sf.json.JsonConfig; | |
import net.sf.json.util.PropertyFilter; | |
public class JsonUtil { | |
private static final ObjectMapper MAPPER = new ObjectMapper(); | |
public JsonUtil() { | |
} | |
public static <T> List<T> jsonToListByJackson(String jsonStr, Class<T> clazz) { | |
JavaType type = MAPPER.getTypeFactory().constructCollectionType(List.class, clazz); | |
Object list = new ArrayList(); | |
try { | |
if (!CollectionUtil.isEmpty(jsonStr)) { | |
list = (List)MAPPER.readValue(jsonStr, type); | |
} | |
return (List)list; | |
} catch (Exception var5) { | |
throw new RuntimeException("json无法解析成包含 " + clazz.getSimpleName() + " 类型的 list"); | |
} | |
} | |
public static String beanToJsonByJackson(Object bean) { | |
String json = ""; | |
try { | |
if (!CollectionUtil.isEmpty(bean)) { | |
json = MAPPER.writeValueAsString(bean); | |
} | |
return json; | |
} catch (Exception var3) { | |
throw new RuntimeException("bean无法转换"); | |
} | |
} | |
public static <T> T jsonToBeanByJackson(String jsonStr, Class<T> clazz) { | |
try { | |
return !CollectionUtil.isEmpty(jsonStr) ? MAPPER.readValue(jsonStr, clazz) : null; | |
} catch (Exception var3) { | |
throw new RuntimeException("json无法解析成 " + clazz.getSimpleName() + " 类型的 bean"); | |
} | |
} | |
public static String mapToJsonArray(Map<String, Object> map) { | |
JSONArray json = JSONArray.fromObject(map); | |
return json.toString(); | |
} | |
public static String mapToJsonObj(Map<String, Object> map) { | |
return JSON.toJSONString(map); | |
} | |
public static String objectToJson(Object object) { | |
StringBuilder json = new StringBuilder(); | |
if (object == null) { | |
json.append("\"\""); | |
} else if (object instanceof String) { | |
json.append("\"").append((String)object).append("\""); | |
} else if (object instanceof Integer) { | |
json.append("\"").append(String.valueOf(object)).append("\""); | |
} else if (object instanceof Date) { | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
json.append("\"").append(formatter.format((Date)object)).append("\""); | |
} else if (object instanceof List) { | |
json.append("\"").append(listToJson((List)object)).append("\""); | |
} else if (object instanceof Map) { | |
json.append(mapToJsonObj((Map)object)); | |
} else { | |
json.append(beanToJson(object)); | |
} | |
return json.toString(); | |
} | |
public static String beanToJson(Object bean) { | |
JSONObject jsonObject = JSONObject.fromObject(bean); | |
return jsonObject.toString(); | |
} | |
public static String beanToJsonNullNotConvert(Object bean) { | |
JsonConfig config = new JsonConfig(); | |
config.setJsonPropertyFilter(new PropertyFilter() { | |
public boolean apply(Object source, String name, Object value) { | |
return value == null || "".equals(value); | |
} | |
}); | |
JSONObject jsonObject = JSONObject.fromObject(bean, config); | |
return jsonObject.toString(); | |
} | |
public static String listToJson(List<?> list) { | |
StringBuilder json = new StringBuilder(); | |
json.append("["); | |
if (list != null && list.size() > 0) { | |
Iterator var2 = list.iterator(); | |
while(var2.hasNext()) { | |
Object obj = var2.next(); | |
json.append(objectToJson(obj)); | |
json.append(","); | |
} | |
json.setCharAt(json.length() - 1, ']'); | |
} else { | |
json.append("]"); | |
} | |
return json.toString(); | |
} | |
public static <T> List<T> jsonToList(String jsonStr, Class<T> clazz) { | |
JSONArray array = JSONArray.fromObject(jsonStr); | |
List<T> list = new ArrayList(); | |
for(int i = 0; i < array.size(); ++i) { | |
JSONObject jsonObject = array.getJSONObject(i); | |
T t = JSONObject.toBean(jsonObject, clazz); | |
list.add(t); | |
} | |
return list; | |
} | |
public static <T> T jsonToBean(String jsonStr, Class<T> clazz) { | |
JSONObject jsonObject = JSONObject.fromObject(jsonStr); | |
return JSONObject.toBean(jsonObject, clazz); | |
} | |
public static <T> T jsonToBeanUseFastJson(String jsonStr, Class<T> clazz) { | |
return JSON.parseObject(jsonStr, clazz); | |
} | |
public static HashMap<String, Object> jsonToMap(Object jsonObj) { | |
HashMap<String, Object> map = new HashMap(); | |
if (CollectionUtil.isEmpty(jsonObj)) { | |
return map; | |
} else { | |
JSONObject json = JSONObject.fromObject(jsonObj); | |
Iterator var3 = json.keySet().iterator(); | |
while(true) { | |
while(var3.hasNext()) { | |
Object k = var3.next(); | |
Object v = json.get(k); | |
if (v instanceof JSONArray) { | |
List<Map<String, Object>> list = new ArrayList(); | |
Iterator it = ((JSONArray)v).iterator(); | |
while(it.hasNext()) { | |
JSONObject json2 = (JSONObject)it.next(); | |
Map<String, Object> tempMap = jsonToMap(json2.toString()); | |
if (!CollectionUtil.isEmpty(tempMap)) { | |
list.add(tempMap); | |
} | |
} | |
if (!CollectionUtil.isEmpty(list) && list.contains((Object)null)) { | |
List<Map<String, Object>> nullCollectionList = new ArrayList(); | |
nullCollectionList.add((Object)null); | |
list.removeAll(nullCollectionList); | |
} | |
map.put(k.toString(), list); | |
} else { | |
map.put(k.toString(), v); | |
} | |
} | |
return map; | |
} | |
} | |
} | |
public static String removeJsonNode(String jsonStr, String key) { | |
if (!CollectionUtil.isEmpty(jsonStr) && !CollectionUtil.isEmpty(key)) { | |
Map<String, Object> tempMap = jsonToMap(jsonStr); | |
if (tempMap.containsKey(key)) { | |
tempMap.remove(key); | |
} | |
return objectToJson(tempMap); | |
} else { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment