java json过滤_通过一个普通javabean,通过一些过滤字段,来生成json

本文介绍了如何使用Java编写工具类,通过注解和反射将JavaBean转换为JSON对象,支持根据指定字段进行过滤。提供了两种方法:一种是通过字段数组过滤,另一种是利用自定义注解@Json进行更精细的控制,包括字段名映射和嵌套结构处理。
摘要由CSDN通过智能技术生成

由于项目中用json作为ExtJS传到程序中的数据格式,所以许多地方需要将javaBean转换成为json,或者将list,map转换为json。所以写了一个工具类来做这个工作。实现方式分为两种:

1. 通过一个普通javabean,通过一些过滤字段,来生成json

Java代码 icon_copy.gif

private static  JSONObject ObjectToJSON(T t, String[] fields, boolean fieldKind){

Field[] fs = t.getClass().getDeclaredFields();

JSONObject jsonObject = new JSONObject();

for (Field field : fs) {

String propertyName = field.getName();

for (String f : fields) {

try {

if (propertyName.equals(f) == fieldKind) {

String methodName = "get"

+ propertyName.substring(0, 1).toUpperCase()

+ propertyName.substring(1);

Method m = t.getClass().getMethod(methodName);

Object o = m.invoke(t);

jsonObject.put(field.getName(), o instanceof String ? transHTML((String)o) : o);

} else {

continue;

}

} catch (SecurityException e) {

throw new JSONUtilException(e);

} catch (NoSuchMethodException e) {

throw new JSONUtilException(e);

} catch (IllegalArgumentException e) {

throw new JSONUtilException(e);

} catch (JSONException e) {

throw new JSONUtilException(e);

} catch (IllegalAccessException e) {

throw new JSONUtilException(e);

} catch (InvocationTargetException e) {

throw new JSONUtilException(e);

}

}

}

return jsonObject;

}

private static JSONObject ObjectToJSON(T t, String[] fields, boolean fieldKind){

Field[] fs = t.getClass().getDeclaredFields();

JSONObject jsonObject = new JSONObject();

for (Field field : fs) {

String propertyName = field.getName();

for (String f : fields) {

try {

if (propertyName.equals(f) == fieldKind) {

String methodName = "get"

+ propertyName.substring(0, 1).toUpperCase()

+ propertyName.substring(1);

Method m = t.getClass().getMethod(methodName);

Object o = m.invoke(t);

jsonObject.put(field.getName(), o instanceof String ? transHTML((String)o) : o);

} else {

continue;

}

} catch (SecurityException e) {

throw new JSONUtilException(e);

} catch (NoSuchMethodException e) {

throw new JSONUtilException(e);

} catch (IllegalArgumentException e) {

throw new JSONUtilException(e);

} catch (JSONException e) {

throw new JSONUtilException(e);

} catch (IllegalAccessException e) {

throw new JSONUtilException(e);

} catch (InvocationTargetException e) {

throw new JSONUtilException(e);

}

}

}

return jsonObject;

}

第一个参数是需要转换的bean,第二个参数为过滤字段,第三个参数是是否需要过滤的字段。也就是说,fieldKind为true时说明生成的json只包含第二个参数中的这些字段。如果fieldKind为false,生成json不包含这些字段

2. 通过在javabean类的属性上用annotation来表示是否需要生成到json中,并且通过可以通过在字段上设置children annotation来定义嵌套

Java代码 icon_copy.gif

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Json {

String jsonName() default "";

boolean children() default false;

String childrenName() default "children";

}

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Json {

String jsonName() default "";

boolean children() default false;

String childrenName() default "children";

}

Java代码 icon_copy.gif

/**

* 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性

* 通过设置jsonName来改变JSONObject的keyName

* 目前不支持对象中有实现Collection接口类型的属性,所以请不要把这种属性加上Annotation

*

* @author (Jessdy) 编写日期:May 9, 2008

* @author (Jessdy) 更新日期:May 23, 2008 更新内容:支持对于同类型的对象的父子层级关系

* @author (Jessdy) 更新日期:Jul 25, 2008 更新内容:对于json输出值的过滤方法

*

* @param 

* @param t

*            需要转换成JSONObject的对象

* @return

*/

@SuppressWarnings("unchecked")

public static  JSONObject ObjectToJSONByAnnotation(T t) {

Field[] fields = t.getClass().getDeclaredFields();

JSONObject jsonObject = new JSONObject();

for (Field field : fields) {

if (field.getAnnotations().length != 0

&& field.getAnnotation(Json.class) != null) {

try {

String propertyName = field.getName();

String methodName = "get"

+ propertyName.substring(0, 1).toUpperCase()

+ propertyName.substring(1);

Method m = t.getClass().getMethod(methodName);

if (!field.getAnnotation(Json.class).children()) {

String keyName = notEmpty(field.getAnnotation(

Json.class).jsonName()) ? field.getAnnotation(

Json.class).jsonName() : field.getName();

Object o = m.invoke(t);

jsonObject.put(keyName, o instanceof String ? transHTML((String)o) : o);

} else {

List children = (List) m.invoke(t);

if (children == null) {

children = new ArrayList();

}

JSONArray jsonArray = children == null ? null

: new JSONArray();

for (T child : children) {

JSONObject jsonChild = ObjectToJSONByAnnotation(child);

jsonArray.put(jsonChild);

}

jsonObject.put(field.getAnnotation(Json.class)

.childrenName(), jsonArray);

}

} catch (SecurityException e) {

throw new JSONUtilException(e);

} catch (NoSuchMethodException e) {

throw new JSONUtilException(e);

} catch (IllegalArgumentException e) {

throw new JSONUtilException(e);

} catch (JSONException e) {

throw new JSONUtilException(e);

} catch (IllegalAccessException e) {

throw new JSONUtilException(e);

} catch (InvocationTargetException e) {

throw new JSONUtilException(e);

}

}

}

return jsonObject;

}

/**

* 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性

* 通过设置jsonName来改变JSONObject的keyName

* 目前不支持对象中有实现Collection接口类型的属性,所以请不要把这种属性加上Annotation

*

* @author (Jessdy) 编写日期:May 9, 2008

* @author (Jessdy) 更新日期:May 23, 2008 更新内容:支持对于同类型的对象的父子层级关系

* @author (Jessdy) 更新日期:Jul 25, 2008 更新内容:对于json输出值的过滤方法

*

* @param

* @param t

* 需要转换成JSONObject的对象

* @return

*/

@SuppressWarnings("unchecked")

public static JSONObject ObjectToJSONByAnnotation(T t) {

Field[] fields = t.getClass().getDeclaredFields();

JSONObject jsonObject = new JSONObject();

for (Field field : fields) {

if (field.getAnnotations().length != 0

&& field.getAnnotation(Json.class) != null) {

try {

String propertyName = field.getName();

String methodName = "get"

+ propertyName.substring(0, 1).toUpperCase()

+ propertyName.substring(1);

Method m = t.getClass().getMethod(methodName);

if (!field.getAnnotation(Json.class).children()) {

String keyName = notEmpty(field.getAnnotation(

Json.class).jsonName()) ? field.getAnnotation(

Json.class).jsonName() : field.getName();

Object o = m.invoke(t);

jsonObject.put(keyName, o instanceof String ? transHTML((String)o) : o);

} else {

List children = (List) m.invoke(t);

if (children == null) {

children = new ArrayList();

}

JSONArray jsonArray = children == null ? null

: new JSONArray();

for (T child : children) {

JSONObject jsonChild = ObjectToJSONByAnnotation(child);

jsonArray.put(jsonChild);

}

jsonObject.put(field.getAnnotation(Json.class)

.childrenName(), jsonArray);

}

} catch (SecurityException e) {

throw new JSONUtilException(e);

} catch (NoSuchMethodException e) {

throw new JSONUtilException(e);

} catch (IllegalArgumentException e) {

throw new JSONUtilException(e);

} catch (JSONException e) {

throw new JSONUtilException(e);

} catch (IllegalAccessException e) {

throw new JSONUtilException(e);

} catch (InvocationTargetException e) {

throw new JSONUtilException(e);

}

}

}

return jsonObject;

}

以上就是主要代码,还有一个自定义的JSONUtilException。另外使用了jsonme.jar包作为json基础包。

Java代码 icon_copy.gif

/**

* 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 目前不支持对象中有实现Collection接口类型的属性

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param t

*            需要转换成JSONObject的对象

* @return

*/

public static  JSONObject ObjectToJSON(T t) {

return ObjectToJSON(t, new String[] { "" });

}

/**

* 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 目前不支持对象中有实现Collection接口类型的属性,所以请把这种属性加入到filter中

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param t

*            需要转换成JSONObject的对象

* @param filters

*            被过滤的属性字符数组

* @return

*/

public static  JSONObject ObjectToJSON(T t, String[] filters) {

return ObjectToJSON(t, filters, false);

}

/**

* 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy)

* 编写日期:Jul 23, 2008

* @author (Jessdy) 更新日期:Jul 25, 2008 更新内容:对于json输出值的过滤方法

*

* @param 

* @param t 需要转换成JSONObject的对象

* @param includes 被包含的属性字符数组

* @return

*/

public static  JSONObject IObjectToJSON(T t, String[] includes){

return ObjectToJSON(t, includes, true);

}

/**

* 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象数组

* @return

*/

public static  JSONArray ArrayToJSON(T[] ts) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(ObjectToJSON(t));

}

return jsonArray;

}

/**

* 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象数组

* @param filters

*            被过滤的属性字符数组

* @return

*/

public static  JSONArray ArrayToJSON(T[] ts, String[] filters) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(ObjectToJSON(t, filters));

}

return jsonArray;

}

/**

* 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象数组

* @param filters

*            被过滤的属性字符数组

* @return

*/

public static  JSONArray IArrayToJSON(T[] ts, String[] includes) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(IObjectToJSON(t, includes));

}

return jsonArray;

}

/**

* 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性

* 通过设置jsonName来改变JSONObject的keyName

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象数组

* @return

*/

public static  JSONArray ArrayToJSONByAnnotation(T[] ts) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(ObjectToJSONByAnnotation(t));

}

return jsonArray;

}

/**

* 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象队列

* @return

*/

public static  JSONArray ArrayToJSON(List ts) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(ObjectToJSON(t));

}

return jsonArray;

}

/**

* 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象队列

* @param filters

*            被过滤的属性字符数组

* @return

*/

public static  JSONArray ArrayToJSON(List ts, String[] filters) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(ObjectToJSON(t, filters));

}

return jsonArray;

}

/**

* 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

*

* @author (Jessdy)

* 编写日期:Jul 23, 2008

*

* @param 

* @param ts 需要转换成JSONArray的对象队列

* @param includes 被包含的属性字符数组

* @return

*/

public static  JSONArray IArrayToJSON(List ts, String[] includes) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(IObjectToJSON(t, includes));

}

return jsonArray;

}

/**

* 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性

* 通过设置jsonName来改变JSONObject的keyName

*

* @author (Jessdy) 编写日期:May 9, 2008

*

* @param 

* @param ts

*            需要转换成JSONArray的对象队列

* @return

*/

public static  JSONArray ArrayToJSONByAnnotation(List ts) {

JSONArray jsonArray = new JSONArray();

for (T t : ts) {

jsonArray.put(ObjectToJSONByAnnotation(t));

}

return jsonArray;

}

/**

* 将Map集合封装成{@link JSONArray}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。

* 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性

* 通过设置jsonName来改变JSONObject的keyName

* @author (Jessdy)

* 编写日期:Jul 25, 2008

*

* @param  键类型

* @param  值类型

* @param ts 需要转换成JSONArray的Map

* @return

*/

public static  JSONArray MapToJSONByAnnotation(Map ts) {

JSONArray jsonArray = new JSONArray();

Iterator keys = ts.keySet().iterator();

while (keys.hasNext()) {

K key = keys.next();

jsonArray.put(ObjectToJSONByAnnotation(ts.get(key)));

}

return jsonArray;

}

/**

*

* @author (Jessdy)

* 编写日期:Jul 25, 2008

*

* @param 

* @param 

* @param ts

* @return

*/

public static  JSONArray MapToJSON(Map ts, String[] filters) {

JSONArray jsonArray = new JSONArray();

Iterator keys = ts.keySet().iterator();

while (keys.hasNext()) {

K key = keys.next();

jsonArray.put(ObjectToJSON(ts.get(key), filters));

}

return jsonArray;

}

/**

*

* @author (Jessdy)

* 编写日期:Jul 25, 2008

*

* @param 

* @param 

* @param ts

* @return

*/

public static  JSONArray IMapToJSON(Map ts, String[] includes) {

JSONArray jsonArray = new JSONArray();

Iterator keys = ts.keySet().iterator();

while (keys.hasNext()) {

K key = keys.next();

jsonArray.put(IObjectToJSON(ts.get(key), includes));

}

return jsonArray;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值