java封装json格式工具类优化

需要調用的主要方法


/**
* @author:作者Lelonta
* @version:1.0
* 创建时间:2017-4-12 下午10:27:50
* 类说明
*/
public class MyJson {

/**
 * 返回成功数据
 * @param resObj
 * @return
 */
public static JSONObject returnSucc(Object resobj){
    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("code", "40000");
    //obj.put("info", "success");
    obj.put("resobj", resobj);

    JSONObject jsonObject = MakeJsonUtil.createJson(obj);
    return jsonObject;
}

/**
 * 返回成功数据 
 * 排除不想要的数据字段
 * @param resobj
 * @param excludes
 * @return
 */
public static JSONObject returnSuccAndExclude(Object resobj,String[] excludes){
    //定义一个map
    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("code", "40000");
    //obj.put("info", "success");
    obj.put("resobj", resobj);

    JSONObject jsonObject = MakeJsonUtil.createJsonExclude(obj, excludes);

    return jsonObject;
}

/**
 * 返回成功数据以及其他相加的数据 
 * 排除不想要的数据字段
 * @param resObj
 * @return
 */
public static JSONObject returnSuccForOtherAndExclude(Object resobj,Object other
        ,String[] excludes){

    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("code", "40000");
    //obj.put("info", "success");
    obj.put("resobj", resobj);
    obj.put("other", other);

    JSONObject jsonObject = MakeJsonUtil.createJsonExclude(obj, excludes); 

    return jsonObject;
}

/**
 * 返回成功数据以及其他想加的数据
 * @param resobj
 * @param other
 * @return
 */
public static JSONObject returnSuccForOther(Object resobj,Object other){

        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("code", "40000");
        //obj.put("info", "success");
        obj.put("resobj", resobj);
        obj.put("other", other);

        JSONObject jsonObject = MakeJsonUtil.createJson(obj);

        return jsonObject;
    }

/**
 * 返回错误结果
 * @param resObj
 * @return
 */
public static JSONObject returnFail(Object resobj){
    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("code", "40004");
    //obj.put("info", "success");
    obj.put("resobj", resobj);

    JSONObject jsonObject = MakeJsonUtil.createJson(obj);

    return jsonObject;
}

/**
 * 返回错误数据
 * 排除不想要的数据字段
 * @param resobj
 * @param excludes
 * @return
 */
public static JSONObject returnFailAndExclude(Object resobj,String[] excludes){
    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("code", "40004");
    //obj.put("info", "success");
    obj.put("resobj", resobj);

    JSONObject jsonObject = MakeJsonUtil.createJsonExclude(obj, excludes); 
    return jsonObject;
}

public static void main(String[] args) {
    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("code", "40000");
    //obj.put("info", "success");
    obj.put("resobj", "resobj");
    obj.put("other", "other");
    JsonConfig jsonConfig = new JsonConfig();  
    jsonConfig.setIgnoreDefaultExcludes(true);  //默认为false,即过滤默认的key  
    jsonConfig.setExcludes(new String[]{"other"});
    jsonConfig.registerJsonValueProcessor(Date.class, 
                            new JsonDateValueProcessor());
    JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig); 

    System.out.println(jsonObject);  
}

}


封裝MakeJsonUtil方法 被myjson引用

//创建 jsonconfig对象
private static JsonConfig jsonConfig = null;

/**
 * 返回jsonobject
 * @param obj
 * @return
 */
public static JSONObject createJson(Map<String, Object> obj) {

    jsonConfig = new JsonConfig();
    jsonConfig.registerJsonValueProcessor(Date.class, 
            new JsonDateValueProcessor());
    JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
    return jsonObject;
}

/**
 * 返回jsonobject,并且去除不想要的字段
 * @param obj
 * @param excludes
 * @return
 */
public static JSONObject createJsonExclude(Map<String, Object> obj,String[] excludes) {

    jsonConfig = new JsonConfig();
    jsonConfig.setExcludes(excludes);
    jsonConfig.registerJsonValueProcessor(Date.class, 
            new JsonDateValueProcessor());
    JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig); 
    return jsonObject;
}

時間格式化 這個類可以從網上找到

public class JsonDateValueProcessor implements JsonValueProcessor {
private String format =”yyyy-MM-dd HH:mm:ss”;

public JsonDateValueProcessor() {
    super();
}

public JsonDateValueProcessor(String format) {
    super();
    this.format = format;
}

public Object processArrayValue(Object paramObject,
        JsonConfig paramJsonConfig) {
    return process(paramObject);
}

public Object processObjectValue(String paramString, Object paramObject,
        JsonConfig paramJsonConfig) {
    return process(paramObject);
}


private Object process(Object value){
    if(value instanceof Date){  
        SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);  
        return sdf.format(value);
    }  
    return value == null ? "" : value.toString();  
}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐观的Terry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值