自己基于fastjson,封装的工具包

package com.circle.core.utilv2;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.circle.core.util.DateUtil;
import com.circle.core.util.RecordException;
import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * 使用Java反射机制完成,Java类转成Json对象的转换
 *
 * @author tianming.fan
 */
public class JSONUtils {


    /**
     * 使用fastjson直接转
     * @param bean
     * @return
     */
    public static JSONObject parse(Object bean) {
        String jsonString = JSONObject.toJSONString(bean);
        RecordException.recordPrintErrMsg("JSONUtils JSONObject parse(Object bean) jsonString:"+jsonString);
        return JSONObject.parseObject(jsonString);
    }

    /**
     * 使用fastjson直接转
     * @param bean
     * @return
     */
    public static  T parse(Object bean,Class clas) {
        String jsonString = JSONObject.toJSONString(bean);
        RecordException.recordPrintErrMsg("JSONUtils JSONObject parse(Object bean) jsonString:"+jsonString);
        return JSONObject.parseObject(jsonString,clas);
    }

    /**
     * 使用fastjson直接转
     * @param list
     * @return
     */
    public static JSONArray parse(List list) {
        String jsonString = JSONArray.toJSONString(list);
        RecordException.recordPrintErrMsg("JSONUtils JSONArray parse(List list) jsonString:"+jsonString);
        return JSONArray.parseArray(jsonString);
    }


    /**
     * 使用fastjson直接转
     * @param list
     * @return
     */
    public static  List parseMapList(List> list, Class entityClass) {
        String jsonString = JSONArray.toJSONString(list);
        RecordException.recordPrintErrMsg("JSONUtils  List parseMapList(List> list, Class entityClass) jsonString:"+jsonString);
        return JSONArray.parseArray(jsonString,entityClass);
    }

    /**
     * 将单个Class转换成单个JSONObject
     *
     * @param bean
     * @return
     */
    public static JSONObject parse2JSONObject(Object bean) {
        JSONObject returnObject = new JSONObject();
        try {
            Field[] fields = bean.getClass().getDeclaredFields();
            for (int j = 0; j < fields.length; j++) {
                Field field = fields[j];
                if ("java.lang.String".equals(field.getType().getName())) {
                    //String 赋值
                    Method method = bean.getClass().getMethod(getGetMethodName(field.getName()),new Class[0]);
                    Object value = method.invoke(bean, new Object[0]);
                    returnObject.put(field.getName(),value);
                }
                if("java.util.Date".equals(field.getType().getName())){
                    Method method = bean.getClass().getMethod(getGetMethodName(field.getName()),new Class[0]);
                    Date value = (Date) method.invoke(bean, new Object[0]);
                    String newDateString = DateUtil.formatDateTimeSSS(value);
                    returnObject.put(field.getName(),newDateString);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnObject;
    }


    public static String getSetMethodName(String fieldName) {
        StringBuffer result = new StringBuffer();
        String methodName = result.append("set")
                .append(fieldName.substring(0, 1).toUpperCase())
                .append(fieldName.substring(1)).toString();

        return methodName;
    }

    public static String getGetMethodName(String fieldName) {
        StringBuffer result = new StringBuffer();
        String methodName = result.append("get")
                .append(fieldName.substring(0, 1).toUpperCase())
                .append(fieldName.substring(1)).toString();

        return methodName;
    }


}

你可能感兴趣的:(java,java)