您的位置:首页 > 编程语言 > Java开发

java封装json格式工具类优化

2017-04-15 07:43 369 查看
需要調用的主要方法

/**

* @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();
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java json