zoukankan      html  css  js  c++  java
  • java json转换(二)

    package com.kps.common.utils;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.TypeReference;
    import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
    import com.alibaba.fastjson.serializer.SerializeConfig;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import java.nio.charset.Charset;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * Created by helka067 on 2018/7/20.
     * json字符串,jsonobject, java对象
     * 
     */
    
    @SuppressWarnings("all")
    public class FastJsonUtils {
        private static final SerializeConfig config =new SerializeConfig();
        private static final SerializerFeature[] features = { SerializerFeature.QuoteFieldNames, // 双引号
                SerializerFeature.WriteMapNullValue, // 输入空值字段
                SerializerFeature.WriteEnumUsingToString, // 枚举输出STRING
                SerializerFeature.WriteNullBooleanAsFalse, // 布尔类型如果为null输出false
                SerializerFeature.WriteNullListAsEmpty, // List字段如果为null输出为[]
                //SerializerFeature.WriteNullNumberAsZero, // number类型如果为null输出0
                //SerializerFeature.WriteNullStringAsEmpty, // 字符串类型如果为null输出""
                SerializerFeature.SortField, // 按字段名称排序后进行输出
                SerializerFeature.WriteDateUseDateFormat// 设置日期格式
        };
    
    
        
    
        // java对象转json
        public static String getBeanToJson(Object object) {
            return JSON.toJSONString(object, config, features);
        }
        
        /**
         * hibernate model存在关联循环,截取层次处理
         * @throws Exception 
         */
        public static String toHJSON(Object object, int... layers) {
            if (object == null)
                return "";
            if (object instanceof List) {
                object = ConverHelper.getListCopy((List) object, layers);
            } else if (object instanceof Set) {
                object = ConverHelper.getSetCopy((Set) object, layers);
            } else if (object instanceof Map) {
                object = ConverHelper.getMapCopy((Map) object, layers);
            } else {
                object = ConverHelper.getCopy(object, layers);
            }
            return JSON.toJSONString(object, config, features);
    
        } 
        
    
        // json转java对象
        public static <T> T getJsonToBean(String json, Class<T> clazz) {
            return JSON.parseObject(json, clazz);
        }
    
        // json转java对象
        public static <T> T getJsonToBean(String json, TypeReference<T> tr) {
            return JSON.parseObject(json, tr);
        }
    
        // json转换为数组
        public static <T> Object[] toArray(String json, Class<T> clazz) {
            return JSON.parseArray(json, clazz).toArray();
        }
    
        // json转换为List
        public static <T> List<T> toList(String json, Class<T> clazz) {
            return JSON.parseArray(json, clazz);
        }
    
        // JsonObject转json
        public static String JsonObjectToJson(JSONObject jo) {
            return jo.toJSONString();
        }
    
        // JsonObject转java对象
        public static <T> T JsonObjectToBean(JSONObject jo, Class<T> clazz) {
            return jo.toJavaObject(clazz);
        }
    
        // JsonObject转java对象
        public static <T> T JsonObjectToBean(JSONObject jo, TypeReference<T> tr) {
            return jo.toJavaObject(tr);
        }
    
        //JsonArray转json
        public static String JsonArrayToJson(JSONArray ja) {
            return ja.toJSONString();
        }
    
        // JsonArray转java对象
        public static <T> List<T> JsonArrayToBean(JSONArray ja, Class<T> clazz) {
            return ja.toJavaList(clazz);
        }
    
        // java对象转jsonobject
        public static JSONObject toJsonObject(Object object) {
            String s = getBeanToJson(object);
            return (JSONObject)JSONObject.toJSON(object);
        }
        
        // 将string转化为序列化的json字符串
        public static Object textToJson(String text) {
            Object objectJson = JSON.parse(text);
            return objectJson;
        }
    
        //map转实体对象
        public static <T> T mapToBean(Map map,Class<T> clazz) {
            String json = getBeanToJson(map);
            return JSON.parseObject(json, clazz);
        }
        
        // map转实体对象
        public static <T> List<T> mapToBean(List<Map> list, Class<T> clazz) {
            String json = getBeanToJson(list);
            return JSON.parseArray(json, clazz);
        }
    }

     ConverHelper 详见:https://www.cnblogs.com/hanjun0612/p/9779781.html

  • 相关阅读:
    linux 回收站 路径
    Linux 让进程在后台可靠运行的几种方法
    用marquee和div+js实现首尾相连循环滚动效果
    轻型数据库SQLite结合PHP的开发
    linux系统权限修复——学生误操作!
    2009级 毕业设计 题目
    linux下硬盘uuid查看及修改设置
    创建网站地图
    用上下左右箭头键在textbox中的光标跳转
    SHELL中时间的比较
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/10330673.html
Copyright © 2011-2022 走看看