• java常用工具类(二)—— JSON处理工具类


    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":"zhangsan@163.com"}";
    
        }
    
    }
    
    • 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"));
        }
    
    }
    
    
  • 相关阅读:
    ASP.NET中JS简单调用webservices的方法
    WINDOWS SERVER 2003使用IIS服务配置WEB站点(转)
    Tomcat安装配置(转)
    javascript 调用.net后台的数据 和方法
    win7下,sql2005安装,提示 iis功能和com+目录要求 的解决
    Vista或windows7远程桌面控制服务器较慢 反应迟钝
    Asp.net Ajax的使用
    记录远程登陆用户的IP
    Ext.menu.Menu 属性及基础应用
    关于VS2005 无法使用切换到设计视图的解决方法
  • 原文地址:https://www.cnblogs.com/LiuSandy/p/9932695.html
Copyright © 2020-2023  润新知