两个对象值转换的方法(BeanUtils.copyProperties与JSONObject.parseObject对比)

将源对象赋值到目标对象方法:
方法一:BeanUtils.copyProperties(源对象, 目标对象); //org.springframework.beans.BeanUtils
方法二:目标对象 =JSONObject.parseObject(JSON.toJSONString(源对象), 目标对象.class); //com.alibaba.fastjson.JSON; com.alibaba.fastjson.JSONObject;

实测结果方法一的耗时为方法二的2-3倍。

附测试代码:

public class TestM {
public static void main(String[] args) {
Testb testa=new Testb();
Testa testb=new Testa();
long a=System.currentTimeMillis();
BeanUtils.copyProperties(testa, testb);
System.err.println("方法一耗时:"+(System.currentTimeMillis()-a));

long b=System.currentTimeMillis();
Testa testc= JSONObject.parseObject(JSON.toJSONString(testb), Testa.class);
System.err.println("方法二耗时:"+(System.currentTimeMillis()-b));

}

测试结果:
方法一耗时:214
方法二耗时:87

关于两者的差异因为没有看到alibaba的JSONObject.parseObject方法的源码而告终,希望各位看官能在评论多多讲解。
原文地址:https://www.cnblogs.com/MQTimor/p/11081124.html