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

由于项目中用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
    评论
Java中,可以使用JSONObject类来将JSON字符串转换JSON对象。可以使用JSONObject的构造函数将JSON字符串作为参数传入,然后使用该对象的方法来解析JSON字符串。以下是一个示例代码: ``` import com.alibaba.fastjson.JSONObject; public class JsonStrToJsonExample { public static void main(String[] args) { String jsonStr = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); System.out.println(jsonObject); } } ``` 在上面的示例中,我们首先定义了一个JSON字符串`jsonStr`,然后使用`JSONObject.parseObject(jsonStr)`方法将该字符串转换为JSON对象`jsonObject`。最后,我们通过打印`jsonObject`来查看转换后的结果。 请注意,上述示例使用了Fastjson库来进行JSON字符串的解析和转换。确保在项目中导入Fastjson库的依赖。 希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [如何将 Java 对象转换为 JSON?](https://blog.csdn.net/allway2/article/details/123619476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java 获取类中所有字段 转换为 json字符串 Java 类中字段转换为json字符串 javabean 字段 转换为 jsonStr](https://blog.csdn.net/HaHa_Sir/article/details/127103702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值