将list对象转换成json格式
2014-08-07 15:45
78 查看
一个简单的工具类,可以将 list 的对象,转换成json格式,进行输出。
性能方面,自己测试:10000条,大概是 3189毫秒。在多了,就不建议使用了。
现贴出源码,共参考:
package com.test.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import com.test.bean.Person; /** * 演示反射的使用: * 通过反射找到方法名,参数名等 * */ public class TestReflectUtil { /** * 根据指定的类,将List集合转换成json格式进行输出。 * 注:只能处理基本类型,以及ByteEnum的实现类,java.util.Date * * @param clazz class对象 * @param col list集合 * @param prefix 产生的json传的前缀,默认为 "list" * @return json格式的字符串 * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws InvocationTargetException * @throws IllegalArgumentException */ public static <T> String listToJson(Class<?> clazz,List<T>col,String prefix) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ if(col==null || col.size()==0 ) return ""; if(prefix==null || prefix.equals("")) prefix = "\"list\""; StringBuilder sb = new StringBuilder("{"+prefix+":["); //使用LinkedHashMap保证输出的顺序一致 LinkedHashMap<String,Method> methodMap = new LinkedHashMap<String, Method>(); //性能优化:第一次获取所有的method以后,缓存到Map,提高效率。 for(int k=0;k<col.size();k++){ Object obj = col.get(k); if(k==0){ Method[] methods = clazz.getMethods(); for(int i=0;i<methods.length;i++){ if(i==0) sb.append("{"); Method method = methods[i]; String methodName = method.getName(); if(methodName.equals("getClass")) continue; if(methodName.startsWith("get")){ String proName = methodName.substring(3,4).toLowerCase()+methodName.substring(4); String proValue = getObjValue(obj, method); sb.append("\""+proName+"\":\""+proValue+"\","); methodMap.put(proName, method); } } } else{ for (String proName : methodMap.keySet()) { String proValue = getObjValue(obj, methodMap.get(proName)); sb.append("\"" + proName + "\":\"" + proValue + "\","); } } String tempStr = sb.substring(0, sb.length()-1); sb = new StringBuilder(tempStr); sb.append("},{"); } String tempStr = sb.substring(0, sb.length()-2); sb = new StringBuilder(tempStr).append("]}"); return sb.toString(); } /** * 获得obj对象,调用method后,获得的方法值。 obj.method() * @param obj * @param method * @return * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException */ public static String getObjValue(Object obj,Method method) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ String proValue = null; Object tempValue = method.invoke(obj,new Object[]{}); if(tempValue instanceof Date){ SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); proValue = formate.format((Date)tempValue); }else{ proValue = tempValue.toString(); } return proValue; } /** * 测试类 * @param args * 测试结果[不使用缓存]:执行10000次,耗时3189毫秒;执行20000次,耗时15157毫秒 * 使用缓存:执行20000次,耗时15283毫秒 * 缓存效果不大。。。 */ public static void main(String[] args) throws Exception{ int count = 20000; List<Person>list = new ArrayList<Person>(); for(int i=0;i<count;i++){ list.add(new Person("张"+i,i)); } long t1 = System.currentTimeMillis(); System.out.println(listToJson(Person.class, list, "list")); long t2 = System.currentTimeMillis(); System.out.println("执行"+count+"次,共耗时:"+(t2-t1)+"毫秒"); } }
有什么意见,欢迎留言讨论 阅读更多
相关文章推荐
- Java中对象的list与JSON格式字符串的互相转换
- java中利用jar包将list,对象等转换为json数据格式
- [转]将List对象列表转换成JSON格式的类
- SpringMVC 中List 对象转换成Json格式 List对象中属性为NUll解决
- 将List对象列表转换成JSON格式的类
- 将List对象列表转换成JSON格式的类实现方法
- [转]将List对象列表转换成JSON格式的类
- JSON 格式的转换: 数组、字符串、List集合、DataTable,实体对象
- Android JSON数据格式的解析,与简单对象、带泛型的List和Map的转换,以及GSON的使用
- json list map 对象 转换成tostring Json的数据格式
- 把List对象或者单值对象转换为Json格式
- 关于json格式存储数据库成bean对象list,取出来再次转换成json格式
- [转]将List对象列表转换成JSON格式的类
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
- 转换json格式的日期为Javascript对象的函数
- java中将数组、对象、Map、List转换成JSON数据
- Json数据转换成Map与List格式
- List转换成JSON对象报错(二)
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
- json格式的字符串转换成了JSON对象的两种方法(eval()和JSON.parse())