通过一个普通javabean,通过一些过滤字段,来生成json
由于项目中用json作为ExtJS传到程序中的数据格式,所以许多地方需要将javaBean转换成为json,或者将list,map转换为json。所以写了一个工具类来做这个工作。实现方式分为两种:
1. 通过一个普通javabean,通过一些过滤字段,来生成json
- private static <T> 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来定义嵌套
- @Target(ElementType.FIELD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface Json {
- String jsonName() default "";
- boolean children() default false;
- String childrenName() default "children";
- }
- /**
- * 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- * 通过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 <T>
- * @param t
- * 需要转换成JSONObject的对象
- * @return
- */
- @SuppressWarnings("unchecked")
- public static <T> 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<T> children = (List<T>) m.invoke(t);
- if (children == null) {
- children = new ArrayList<T>();
- }
- 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基础包。
- /**
- * 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有get方法且符合JavaBean的命名标准。
- * 目前不支持对象中有实现Collection接口类型的属性
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param t
- * 需要转换成JSONObject的对象
- * @return
- */
- public static <T> JSONObject ObjectToJSON(T t) {
- return ObjectToJSON(t, new String[] { "" });
- }
- /**
- * 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- * 目前不支持对象中有实现Collection接口类型的属性,所以请把这种属性加入到filter中
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param t
- * 需要转换成JSONObject的对象
- * @param filters
- * 被过滤的属性字符数组
- * @return
- */
- public static <T> JSONObject ObjectToJSON(T t, String[] filters) {
- return ObjectToJSON(t, filters, false);
- }
- /**
- * 将对象封装成{@link JSONObject}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy)
- * 编写日期:Jul 23, 2008
- * @author (Jessdy) 更新日期:Jul 25, 2008 更新内容:对于json输出值的过滤方法
- *
- * @param <T>
- * @param t 需要转换成JSONObject的对象
- * @param includes 被包含的属性字符数组
- * @return
- */
- public static <T> JSONObject IObjectToJSON(T t, String[] includes){
- return ObjectToJSON(t, includes, true);
- }
- /**
- * 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象数组
- * @return
- */
- public static <T> JSONArray ArrayToJSON(T[] ts) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(ObjectToJSON(t));
- }
- return jsonArray;
- }
- /**
- * 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象数组
- * @param filters
- * 被过滤的属性字符数组
- * @return
- */
- public static <T> JSONArray ArrayToJSON(T[] ts, String[] filters) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(ObjectToJSON(t, filters));
- }
- return jsonArray;
- }
- /**
- * 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象数组
- * @param filters
- * 被过滤的属性字符数组
- * @return
- */
- public static <T> JSONArray IArrayToJSON(T[] ts, String[] includes) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(IObjectToJSON(t, includes));
- }
- return jsonArray;
- }
- /**
- * 将对象数组封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- * 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性
- * 通过设置jsonName来改变JSONObject的keyName
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象数组
- * @return
- */
- public static <T> JSONArray ArrayToJSONByAnnotation(T[] ts) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(ObjectToJSONByAnnotation(t));
- }
- return jsonArray;
- }
- /**
- * 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象队列
- * @return
- */
- public static <T> JSONArray ArrayToJSON(List<T> ts) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(ObjectToJSON(t));
- }
- return jsonArray;
- }
- /**
- * 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象队列
- * @param filters
- * 被过滤的属性字符数组
- * @return
- */
- public static <T> JSONArray ArrayToJSON(List<T> ts, String[] filters) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(ObjectToJSON(t, filters));
- }
- return jsonArray;
- }
- /**
- * 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- *
- * @author (Jessdy)
- * 编写日期:Jul 23, 2008
- *
- * @param <T>
- * @param ts 需要转换成JSONArray的对象队列
- * @param includes 被包含的属性字符数组
- * @return
- */
- public static <T> JSONArray IArrayToJSON(List<T> ts, String[] includes) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(IObjectToJSON(t, includes));
- }
- return jsonArray;
- }
- /**
- * 将对象队列封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- * 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性
- * 通过设置jsonName来改变JSONObject的keyName
- *
- * @author (Jessdy) 编写日期:May 9, 2008
- *
- * @param <T>
- * @param ts
- * 需要转换成JSONArray的对象队列
- * @return
- */
- public static <T> JSONArray ArrayToJSONByAnnotation(List<T> ts) {
- JSONArray jsonArray = new JSONArray();
- for (T t : ts) {
- jsonArray.put(ObjectToJSONByAnnotation(t));
- }
- return jsonArray;
- }
- /**
- * 将Map集合封装成{@link JSONArray}对象,对象中的每个属性必须有<b>get</b>方法且符合<b>JavaBean</b>的命名标准。
- * 通过Annotation来封装JSONObject,JSONObject只包含带有{@link @Json}注释的属性
- * 通过设置jsonName来改变JSONObject的keyName
- * @author (Jessdy)
- * 编写日期:Jul 25, 2008
- *
- * @param <K> 键类型
- * @param <T> 值类型
- * @param ts 需要转换成JSONArray的Map
- * @return
- */
- public static <K, T> JSONArray MapToJSONByAnnotation(Map<K, T> ts) {
- JSONArray jsonArray = new JSONArray();
- Iterator<K> 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 <K>
- * @param <T>
- * @param ts
- * @return
- */
- public static <K, T> JSONArray MapToJSON(Map<K, T> ts, String[] filters) {
- JSONArray jsonArray = new JSONArray();
- Iterator<K> 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 <K>
- * @param <T>
- * @param ts
- * @return
- */
- public static <K, T> JSONArray IMapToJSON(Map<K, T> ts, String[] includes) {
- JSONArray jsonArray = new JSONArray();
- Iterator<K> keys = ts.keySet().iterator();
- while (keys.hasNext()) {
- K key = keys.next();
- jsonArray.put(IObjectToJSON(ts.get(key), includes));
- }
- return jsonArray;
- }

浙公网安备 33010602011771号