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 ""

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值