自己编写的返回json串的工具类
用着别人封装的东西而且返回的数据有些都不需要还不能处理,感觉着实不爽,因此开始接触jsonconfig,顺势写出这个工具类,工具类其实都大同小异,写这个只是出于个人爱好!仅此而已!!!
/**
* @author:作者Lelonta:
* @version:1.0
* 创建时间:2017-4-12 下午10:27:50
* 类说明
*/
public class MyJson {
//定义一个jsonconfig对象
private static JsonConfig jsonConfig = null;
/**
* 返回成功数据
* @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);
jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
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);
jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
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);
jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
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);
jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
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);
jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
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);
jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
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 = 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);
}
}