一个简单的工具类,可以将 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)+"毫秒");
}
}
有什么意见,欢迎留言讨论