java常用工具類(二)—— JSON處理工具類
阿新 • • 發佈:2018-11-09
tor ast val simple sta 轉換 local pass password
package com.springboot.commons.utils; import com.springboot.commons.scan.JacksonObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.io.IOException; import java.util.Iterator; /** * @author Lius * @date 2018/10/26 14:34 * @description JSON 工具類 */ public class JsonUtils { private static final String JSONARRAY = "net.sf.json.JSONArray"; public JsonUtils() { } private static ObjectMapper getInstance() { return JacksonHolder.INSTANCE; } private static class JacksonHolder { private static ObjectMapper INSTANCE = new JacksonObjectMapper(); } /** * 將對象序列化成JSON對象 * * @param object java對象 * @author Lius * @date 2018/10/26 14:37 */ public static String toJson(Object object) { try { return getInstance().writeValueAsString(object); } catch (JsonProcessingException e) { throw new RuntimeException(); } } /** * 將json反序列化成java對象 * * @param jsonString 需要序列化的json字符串 * @param valueType 序列化成的對象 * @author Lius * @date 2018/10/26 14:42 */ public static <T> T parse(String jsonString, Class<T> valueType) { try { return getInstance().readValue(jsonString, valueType); } catch (IOException e) { throw new RuntimeException(); } } /** * 判斷所有json字段是否全為空 * * @param jsonObj JSON 對象 * @author Lius * @date 2018/10/26 14:51 */ public boolean isAllFieldNotNull(Object jsonObj) throws Exception { boolean flag = false; if (jsonObj == null) { return false; } String type = jsonObj.getClass().getName(); // Object對象 if (!JSONARRAY.equals(type)) { JSONObject jsonObject = JSONObject.fromObject(jsonObj); Iterator iterator = jsonObject.keys(); while (iterator.hasNext()) { String key = (String) iterator.next(); if (jsonObject.get(key) != null && !"".equals(jsonObject.get(key))) { flag = true; } } return flag; } else { JSONArray jsonArray = JSONArray.fromObject(jsonObj); for (Object object : jsonArray) { JSONObject jsonObject = JSONObject.fromObject(object); Iterator iterator = jsonObject.keys(); while (iterator.hasNext()) { String key = (String) iterator.next(); if (jsonObject.get(key) != null && !"".equals(jsonObject.get(key))) { flag = true; } } return flag; } } return true; } /** * 把JSON字符串轉換為Map形式 * @author Lius * @date 2018/11/8 9:19 */ public static Map<String, Object> jSON2Map(String jsonStr) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject json = JSONObject.fromObject(jsonStr); for (Object k : json.keySet()) { Object v = json.get(k); if (v instanceof JSONArray) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator it = ((JSONArray) v).iterator(); while (it.hasNext()) { Object json2 = it.next(); list.add(jSON2Map(json2.toString())); } map.put(k.toString(), list); } else { map.put(k.toString(), v); } } return map; } public static void main(String[] args) { String json = "{\"name\":\"zhangsan\",\"age\":20,\"loginName\":\"loginName\",\"password\":\"[email protected]\"}"; } }
- JacksonObjectMapper 代碼如下
package com.springboot.commons.scan; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Locale; import java.util.TimeZone; /** * 解決Jackson 差8小時的問題 * @author Lius */ @Component("jacksonObjectMapper") public class JacksonObjectMapper extends ObjectMapper { private static final long serialVersionUID = 4288193147502386170L; private static final Locale CHINA = Locale.CHINA; public JacksonObjectMapper() { this.setLocale(CHINA); this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", CHINA)); this.setTimeZone(TimeZone.getTimeZone("GMT+8")); } }
java常用工具類(二)—— JSON處理工具類